2012-04-19 85 views
0

我有一个视图控制器,单击按钮时显示一个表视图控制器。一切工作正常表视图委托,表视图显示很好,但在ellForRowAtIndexPath:委托方法,单元格被实例化并返回,但它没有正确显示。表视图不显示自定义单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"alrededor"; 

    alrededorCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]]; 

    NSLog(@"categoria %@", categoria); 

    cell.title.text = [categoria valueForKey:@"title"]; 

    return cell; 

} 

非常感谢

+0

你是什么意思**无法正确显示**? – 2012-04-19 11:00:37

回答

1

如果您要加载的单元格是UITableViewCell的子类,并且您使用了界面生成器来构建单元格,则必须执行一些操作。

在nib文件中,删除创建它时存在的视图,添加UITableViewCell并将其类更改为alrededorCell。更改单元类而不是文件的所有者。当你连接按钮,标签等。一定要将它们链接到单元格而不是文件的所有者。还要将单元格uniqueIdentifier设置为alreded或。

在的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"alrededor"; 

    alrededorCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil]; 
     for (alrededorCell *view in xib) { 
      if ([view isKindOfClass:[alrededorCell class]]) { 
       cell = view; 
      } 
     } 
    } 


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]]; 

    NSLog(@"categoria %@", categoria); 

    cell.title.text = [categoria valueForKey:@"title"]; 

    return cell; 

} 
2

为什么你创建你的这个样子?

if (cell == nil) { 
    cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

如果它是您自己的自定义单元格为什么使用UITableViewCellStyleDefault?

+0

嗨,哪一个是正确的定制?谢谢 – theomen 2012-04-19 11:04:16

+0

请查看http://howtomakeiphoneapps.com/how-to-design-a-custom-uitableviewcell-from-scratch/1292/ – 2012-04-19 11:07:31

相关问题