【问题标题】:Adding an extra cell to the top of a UITableView in Cocoa在 Cocoa 中的 UITableView 顶部添加一个额外的单元格
【发布时间】:2010-10-27 14:24:38
【问题描述】:

我有一个从 NSManagedObjects 数组生成的表。 这工作正常,直到我尝试在表格顶部添加一个额外的单元格。

这是我目前正在尝试的:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int menuLength = [mainMenu count] + 1;
    return menuLength;
}

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

    // Check for reusable cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"UITableViewCell"];


    // If no reusable cell, create one
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                   reuseIdentifier: @"UITableViewCell"] autorelease];
    }

    // Set the text on the cell with the genus name that is at the nth index of generaList
    if ([indexPath row] == 0) {
        [[cell textLabel] setText: @"All"];
    } else {
        NSManagedObject *filter = [mainMenu objectAtIndex: [indexPath row]];
        [[cell textLabel] setText: [filter valueForKey: @"filter_label"]];
    }
    return cell;
}

当我尝试滚动表格时,它会引发以下异常:

'NSRangeException',原因:'* -[_PFArray objectAtIndex:]: index (9) beyond bounds (9)'

任何帮助将不胜感激!

【问题讨论】:

  • 顺便说一句,-tableView:numberOfRowsInSection: 返回 10。

标签: objective-c iphone uitableview cocoa-touch uikit


【解决方案1】:

试试这个:

int row = [indexPath row];
if (row == 0) {
    [[cell textLabel] setText: @"All"];
} 
else {
   NSManagedObject *filter = [mainMenu objectAtIndex: row-1];

这里的重要变化是在将它放入 objectAtIndex 之前从行中减去一个 - 我认为你没有这样做是给你异常的原因,因为它在 mainMenu 的索引 9 处查找对象。

当您不止一次使用某些东西时,将行设置为自己的变量是个好主意。在这种情况下,它可能可以帮助您更轻松地发现错误。

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多