Ios 在JSON数组中循环并将字符串输入分组UITableView
我无法将字符串“handle”输入到我拥有的组UITableView中Ios 在JSON数组中循环并将字符串输入分组UITableView,ios,json,uitableview,Ios,Json,Uitableview,我无法将字符串“handle”输入到我拥有的组UITableView中 for(NSDictionary * dataDict in servers) { NSString * handle [dataDict objectForKey:@"handle"]; } 我完全搞不懂如何利用这个for语句动态地创建带有句柄的新单元格 实际的UITableView位于主视图控制器上,没有任何单元格。如何循环遍历这些值并创建带有标签的单元格 -(NSInteger)numberOfSe
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:@"handle"];
}
我完全搞不懂如何利用这个for语句动态地创建带有句柄的新单元格
实际的UITableView位于主视图控制器上,没有任何单元格。如何循环遍历这些值并创建带有标签的单元格
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
mycustomcell *cell = (mycustomcell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mycustomcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog( @"NIB = %@",nib);
NSLog( @"Cell = %@",cell);
}
cell.lblName.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Name"];
cell.lblId.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Id"];
cell.lblGender.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Gender"];
cell.lblAddress.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Address"];
cell.lblPhone.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Phone"];
cell.lblEmail.text = [[arrData objectAtIndex:indexPath.row] objectForKey:@"Email"];
NSLog(@"tbl %@",cell.lblName.text);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
这里mycustomcell是自定义单元格而不是tableview的单元格。。。我希望这对你有用。。使用名为mycustomecell的.h、.m和.xib.创建新的custome单元格您应该使用UITableViewDataSource协议中提供的以下方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
您必须为您的
UITableView
覆盖此数据源方法,不要忘记为表设置数据源。只要在填充服务器阵列后重新加载数据,[table reloadData]代码>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return servers.count; //will create cell based on your array count
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//show handle stirng from server array
cell.textlabel.text = [[server objectAtIndex:indexPath.row]objectForKey:@"handle"];
return cell;
}
您不再需要检查单元格是否为nil并加载nib,因为dequeueReusableCellWithIdentifier已经这样做了:)!Raphael错了:dequeueReusableCellWithIdentifier:
对于未缓存的单元格仍然返回nildequeueReusableCellWithIdentifier:forIndexPath:
将为未缓存的单元格装箱一个新的单元格对象。@Raphael Oliveira,我想你错了,dequeueReusableCellWithIdentifier
如果在可重用单元格队列中找不到对象,则返回nil
。请参阅文档。从文档中可以看到:“如果现有单元格可用,则此方法将其出列,或者使用您以前注册的类或nib文件创建一个新单元格。”是,如果没有可重用的单元格且您未注册类或nib文件,则:,此方法返回nil。
因此检查nil
返回不是可选的!我不知道我把tableview的数据源挂在哪里