0

tableview 中的输出应该在每一行中,我在一行中得到整个响应,其余行是空白的。所以如何在 uitableview 中显示来自服务器的数据的每一行我在 tableview 中得到如下所示的 o/p

aa,bbb,cc……</p>

你好,你好,"",…</p>


o/p 应如下所示

你好


bbb

你好


抄送

“”


这是我的回复 [{"result":[{"request_id":1,"ticket_number":"P_101","email":"xx","user_id":4,"description":"fjdyhsrwgk","status ":"initiated"},{"request_id":2,"ticket_number":"P_102","email":"yyy","user_id":4,"description":"hi","status":"initiated "},{"request_id":3,"ticket_number":"P_103","email":"aaa","user_id":4,"description":"hii","status":"initiated"},{ "request_id":4,"ticket_number":"P_104","email":"bbb","user_id":4,"description":"aa..","status":“发起”}]}]

如何在结果键中获取对象数组

下面是代码

listOfCustomers = [[NSMutableArray alloc]init];

str= [[NSUserDefaults standardUserDefaults] valueForKey:@"user_id"];

NSString *strJson=[NSString stringWithFormat:@"userId=%@",str];
NSString *strlength=[NSString stringWithFormat:@"%d",[strJson length]];
NSURL *url=[NSURL URLWithString:@"url"];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request addValue:strlength forHTTPHeaderField:@"Content-Length"];
NSData *requestData=[strJson dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
NSError *err;
NSData *responseData=[NSURLConnection  sendSynchronousRequest:request returningResponse:nil error:&err];

NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];


NSLog(@"response is %@",strResponse);

dict1=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];

NSArray *results = [dict1 valueForKey:@"result"];

listOfCustomers = [NSMutableArray array];

for (NSDictionary * oneCustomer in results)
{
    // Create a new Customer record
    Attributes * newCustomer = [[Attributes alloc] init];
    newCustomer.Customer_ID = [oneCustomer valueForKey:@"request_id"];
    newCustomer.Company_Name = [oneCustomer valueForKey:@"ticket_number"];
    newCustomer.City = [oneCustomer valueForKey:@"email"];
    newCustomer.userId = [oneCustomer valueForKey:@"user_id"];
    newCustomer.description = [oneCustomer valueForKey:@"description"];
    newCustomer.createdTime = [oneCustomer valueForKey:@"createdTime"];
    newCustomer.Status = [oneCustomer valueForKey:@"status"];
    // Add our new Customer record to our NSMutableArray
    [listOfCustomers addObject:newCustomer];
}

[tableVwTotalRequests reloadData];

   }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listOfCustomers count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
Attributes* cust = [listOfCustomers objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@",cust.Company_Name];
cell.detailTextLabel.text= [NSString stringWithFormat:@"%@",cust.description];
return cell;
}
4

2 回答 2

0

尝试这个,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}

Attributes* cust = [listOfCustomers objectAtIndex:indexPath.row];

NSString *companyName = [NSString stringWithFormat:@"%@",cust.Company_Name];
NSArray *companyNames = [companyName componentsSeparatedByString:@","];
cell.textLabel.text = [companyNames objectAtIndex:indexPath.row];

NSString *description = [NSString stringWithFormat:@"%@",cust.description];
NSArray *descriptions = [description componentsSeparatedByString:@","];
cell.detailTextLabel.text= [descriptions objectAtIndex:indexPath.row];

}
于 2013-10-16T12:28:17.590 回答
0

您将标识符定义为@“cell”,但在您的单元初始化器中,您传递了@“identifier”的重用标识符。这些应该是相同的,而是传递变量,而不是变量的名称作为字符串。

您也错过了在方法结束时返回您的单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
Attributes* cust = [listOfCustomers objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@",cust.Company_Name];
cell.detailTextLabel.text= [NSString stringWithFormat:@"%@",cust.description];

return cell;
}
于 2013-10-16T12:33:23.060 回答