2012-07-05 53 views
3

Custom UITableViewCell in UITableView自定义的UITableViewCell不会在故事板

enter image description here

在这个屏幕截图显示的标签,你可以看到,我已经加入的UITableView的UIViewController中然后在它添加一些标签定制的UITableViewCell。但问题是当我运行应用程序。所有的单元格都是空的。根本没有标签。

Empty Cells

我没有得到什么可以是问题。我搜索了网页并阅读了教程,但无法解决它。

+1

你是如何加入细胞表视图?你可以粘贴你的表格视图委托方法。 –

回答

4

我经过一番努力就自己解决了这个问题。
其实当你创建一个自定义单元格,你必须做这些事情:

  1. 设置标签,图像等的故事板细胞。
  2. 创建自定义单元类(从UITableViewCell继承)(CustomCell.h & .m),CustomCell.h具有所有属性,如标签,图像等iboutlet,并在实现中将它们全部合成。
  3. 创建此自定义类后,回到故事板,选择自定义单元格,将其类更改为CustomCell并给它一些标识符,如“MyCustomCell”,然后右键单击自定义单元格并将IBOutlets连接到标签等。
  4. 现在在要实现UITableView的类中导入CustomCell类,并使用您在CustomCell类中定义的属性。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *MyIdentifier = @"MyCustomCell"; 
    
        CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    
        if (cell == nil) 
        { 
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:MyIdentifier] autorelease]; 
        } 
    
        // Here we use the provided setImageWithURL: method to load the web image 
        // Ensure you use a placeholder image otherwise cells will be initialized with no image 
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"] 
            placeholderImage:[UIImage imageNamed:@"placeholder"]]; 
         cell.myCustomLabel.text = @"My Text"; 
        return cell; 
    } 
    

我做这一切,我的问题就解决了请不要忘记连接代表&数据源和表。

希望这会帮助他人。

+0

不应该[UITableViewCell alloc]实际上是[CustomCell alloc] –

+0

重要的部分是单元标识符。确保它在故事板和tableView:cellForRowAtIndexPath方法中都是相同的。 –

0

在tableview的委托方法中,cellforrowatindexpath里面有一个uitableviewcell,在这个单元格的初始化中应该有一个标识符。可能是“cell”或“c​​ellIdentifier”。

您只需从故事板中选择您的单元格,然后将此标识符字符串输入故事板,其中uitableviewcell的属性检查器保留在其中。

希望这有助于.. :)

1

这是有点晚了,但你可以从你的故事板设置您的视图清晰颜色的背景色解决您的问题。

0

在故事板@“细胞”

第一组小区标识符然后设置标签的标签

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

// Create 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
} 



UILabel *lblDate = (UILabel*)[cell viewWithTag:101]; 
UILabel *lblDistance = (UILabel*)[cell viewWithTag:102]; 
UILabel *lbltype = (UILabel*)[cell viewWithTag:103]; 

lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:@"date"] stringByReplacingOccurrencesOfString:@"-" withString:@"/"]]; 
lblDistance.text = [NSString stringWithFormat:@"%@ KM",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]]; 

NSString *type = [NSString stringWithFormat:@"%@",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]]; 
if([type isEqualToString:@"0"]) 
{ 
    lbltype.text = @"Personal"; 
} 
else 
{ 
    lbltype.text = @"Bussiness"; 
} 
// Configure 
return cell; 
} 
相关问题