我无法将字符串“句柄”输入到我拥有的 UITableView 组中;
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:@"handle"];
}
我完全不知道如何利用这个 for 语句来动态创建带有句柄的新单元格。
实际的 UITableView 位于主视图控制器上,没有任何单元格。如何遍历值并创建带有标签的单元格?
我无法将字符串“句柄”输入到我拥有的 UITableView 组中;
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:@"handle"];
}
我完全不知道如何利用这个 for 语句来动态创建带有句柄的新单元格。
实际的 UITableView 位于主视图控制器上,没有任何单元格。如何遍历值并创建带有标签的单元格?
您必须为您的 覆盖此数据源方法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;
}
-(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 是 Custome 单元格而不是 tableview 的单元格...我希望这对您有用.. 使用 .h 、 .m 和 .xib 创建名为 mycustomecell 的新客户单元格..
您应该使用 UITableViewDataSource 协议上可用的以下方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath