【问题标题】:uilabel from custom tableview cell is not displayed in tableviewcontroller自定义表格视图单元格中的 uilabel 未显示在表格视图控制器中
【发布时间】:2013-03-14 02:54:09
【问题描述】:

我创建了一个表格视图控制器和自定义单元格。我创建了一个新的 tableviewCell 文件,其中包含自定义单元格中标签的出口。

我已经在 tableviewController 中导入了 tableviewcell 类,并尝试通过以下代码将数组中的值分配给单元格:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

cell.name.text= [data objectAtIndex:indexPath.row];

cell.name ------------(tableviewcell类中的uilabel的名称不显示,即使在帮助中也不存在!,我有不知道为什么)

下面的代码通过标记标签来工作

    // Configure the cell...

//    UILabel *label = (UILabel *)[cell viewWithTag:111];
//              label.text= [data objectAtIndex:indexPath.row];

我的问题是如何在没有标记的情况下使插座部分工作以及为什么 uilabel 不显示在 tableviewCONtroller 中?

请帮帮我。非常感谢您的帮助。

提前致谢。

【问题讨论】:

    标签: ios ios5 ios6 uitableview


    【解决方案1】:

    如果您的自定义单元格的类名是“MyCustomTableCell”,它是 UITableViewCell 的子类,那么代码必须如下所示

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

    然后你可以访问它的属性

    cell.name = @"cell name";
    

    【讨论】:

    • 我有另一个插件查询 - :MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    • 和 MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    • 我在网上都看到过,但不知道上面的例子使用哪一个,目前与上面的代码一起使用。请告诉我区别?
    • dequeueReusableCellWithIdentifier:indexPath 是新的 iOS 6 register-and-dequeue 系统。我认为这是告诉 iOS 在 iOS 上使用新功能的方式,indexPath 并不重要......这个方法永远不会返回 nil,它总是会返回一些东西..
    • 是否要根除这个:(有时当我上下滚动时,单元格会以不同的(随机)模式重新组织,例如:1、2、3、4、5:将是 2 ,5,4,1,3 类似的东西。我该如何解决。再次感谢您的时间。)
    【解决方案2】:

    记得加载Nib:

    static NSString *CellIdentifier = @"CellIdentifier";
     MyCustomTableCell *cell = (MyCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
            cell = [[MyCustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil] lastObject];
           // UINib *theNib = [UINib nibWithNibName:@"MyCustomTableCell" bundle:nil];
           // cell = [[theNib instantiateWithOwner:self options:nil] lastObject];
        }
    

    【讨论】:

    • 我有另一个插件查询 - 有什么区别:MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];和 MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];我已经在网上看到过,但不知道上面的示例使用哪一个,目前与上面的代码一起使用。请告诉我有什么区别?
    • 先生,有时当我上下滚动时,单元格会以不同的(随机)模式重新组织,例如:1,2,3,4,5:将是 2,5 ,4,1,3 类似的东西。我该如何解决。再次感谢您的时间。
    • 检查 UITableView.h,你会看到两种方法之间的不同。一个用于 iOS5,另一个用于 iOS6
    • 你能简单解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多