【问题标题】:Custom code for UITableViewCellUITableViewCell 的自定义代码
【发布时间】:2012-10-27 22:29:48
【问题描述】:

我想知道在哪里输入自定义代码来更改 UITableViewCell 的 Label 属性的值。

我不确定这是如何加载的,因为我在 ViewDidLoad 和 (id)initWithStyle 实例方法中放置了一个 NSLog,但都没有写入日志。

我已经设置了一个 NIB 和自定义类都正确链接,并且标签作为属性链接并且不再导致错误。但我无法设置文本。

这是自定义单元格的调用方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
LeftMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"LeftMenuTableViewCell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (LeftMenuTableViewCell*)view;

        }
    }
}

return cell;
}

这是 IMP 文件中 LeftMenuViewCell 类的代码。

-(void)viewDidLoad {

displayName.text = [self.user objectForKey:@"displayName"];

我可以将 displayName 设置为字符串,这也不会改变。如果我将 NSLog 添加到自定义单元格类的 viewDidLoad 中,它不会显示,就像它没有加载一样,但单元格已加载...?

【问题讨论】:

  • 你想知道如何用数据填充UITableView吗?

标签: ios ios5 uitableview


【解决方案1】:

没有代码细节,我只能给出一个模糊的答案。

您的自定义单元格将需要子类化UITableViewCell,并且当数据源方法tableView:cellForRowAtIndexPath: 时,您需要为您的表格提供此自定义子类。

我建议阅读如何使用UITableViews 添加/使用单元格: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

【讨论】:

  • 我已在问题中添加了其他信息,包括代码示例
【解决方案2】:

假设您有自定义 UITableViewCell 和 UILabel,称为 testLabel。如果您的 NIB 和自定义类正确链接,则可以使用以下代码:

MyTableViewCell.h

@interface MyTableViewCell : UITableViewCell

@property (nonatomic, assign) IBOutlet UILabel *testLabel;

@end

您的 UITableViewController 或 UIViewController 中的 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      static NSString *CellIdentifier = @"MyTableViewCellId";
      MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

      if (cell == nil) {
          NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
         cell = [topLevelObjects objectAtIndex:0];    
      }

      [cell.testLabel.text = [_dataSource objectAtIndex:[indexPath row]]];

      return cell;
}

希望对你有帮助:)

【讨论】:

    【解决方案3】:

    例如

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
     RouteCell *routeCell = [self.tableView dequeueReusableCellWithIdentifier:routeIdentifier];
    
    if (routeCell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RouteCell" owner:nil options:nil];
        routeCell = [nib objectAtIndex:0];
    }
    
    routeCell.travelTime.text = @"Here you are setting text to your label";
    
    return routeCell;
    

    【讨论】:

    • 谢谢,我已在问题中添加了代码示例。我知道这可以在对单元格本身的调用中设置,但我希望代码位于实际自定义单元格类的 imp 文件中,因为我将使用多个自定义单元格
    • 自定义单元格没有 viewDidLoad 方法,它只适用于 UIViewController。您应该在 cellForRowAtIndexPath 委托方法中更新单元格的出口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多