我无法将字符串“handle”输入到我拥有的UITableView组中;
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:@"handle"];
}
我完全不知道如何利用this语句动态创建带句柄的新单元格。
实际的UITableView位于主视图控制器上,没有任何单元格。如何循环使用值并使用标签创建单元格?
答案 0 :(得分:1)
您要为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;
}
答案 1 :(得分:0)
-(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;
}
答案 2 :(得分:0)
您应该使用UITableViewDataSource协议上提供的以下方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath