【问题标题】:My custom cell does not display我的自定义单元格不显示
【发布时间】:2012-12-13 13:16:01
【问题描述】:

我创建了一个包含标签的自定义单元格,但是当我在我的表格视图中添加它而不显示时,我的视图控制器不是TableViewController 我已经使用 IB 设置了表格视图并正确设置了它的委托和数据源.

我是这样做的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ExampleAppDataObject* theDataObject = [self theAppDataObject];
    return theDataObject.myAudio.count;
}


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

        static NSString *CellIdentifier = @"Cell";

        ExampleAppDataObject* theDataObject = [self theAppDataObject];

        NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

        NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

        NSString *filename = [parts lastObject];

        AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        }

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        cell.audioName.text = filename;

        return cell;

    }

【问题讨论】:

  • 您的 numberOfSectionsInTableView: 和 tableView:numberOfRowsInSection: 方法是否返回正确的数字?
  • 用代码tableView:numberOfRowsInSection发布方法
  • 检查您的 Ary 中是否有一些您在 tableView 中操作的数据
  • 伙计们,它显示单元格但不显示我的自定义单元格

标签: iphone ios custom-cell


【解决方案1】:

首先检查tableView:numberOfRowsInSection方法中设置了多少行

像下面这样设置自定义单元格后..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];


    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AudioInfoCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (AudioInfoCell *) currentObject;
                break;

            }
        }
    }

    ExampleAppDataObject* theDataObject = [self theAppDataObject];

    NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

    NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

    NSString *filename = [parts lastObject];

    cell.audioName.text = filename;
    return cell;
}

【讨论】:

  • 你为什么要完成所有这些工作?该 for-in 循环将为显示的每个单元格运行,并且每次用户滚动电视时都会运行。
  • 它只是在 cell 为 nil dude 时分配 .. 我使用 custem cell 和这个代码流 dude..
  • 这给了我错误“由于未捕获的异常 'NSUnknownKeyException' 导致应用程序终止,原因:'[ setValue:forUndefinedKey:]: 这个类不是键值编码兼容键 audioName .'"
  • AudioTableViewController 不是 UITableViewController 我这样命名的。
【解决方案2】:

要检查两件事:

  1. 确保重用标识符与 Interface Builder 中的标识符相匹配
  2. 确保您的numberOfRowsInSectionnumberOfSectionsInTableView 返回正确的数字

另外 - 我不使用:

if (cell == nil) {
        cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}

用于实例化单元格。这应该在您实现的 AudioCell TableViewCell 子类中处理。我如何实现这种自定义单元格的一个例子是:

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

    cell.tripIDLabel.text = @"Some TripID";
    cell.hospitalLabel.text = @"Some Hospital";
    cell.cityLabel.text = @"Some City";
    return cell;
}

【讨论】:

  • 但是这个 FlightPickerCell 是否应该重用于其他数据,因为我想将此单元重用于不同类型的数据。
  • 是的。这只是我对子类 UITableViewCell 的实现。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多