2011-11-27 94 views
1

我的问题是相似的这家伙有什么:一个更细胞加入的tableView

Add a last cell to UItableview

其实我使用这得到了在这个问题中选择的相同方法,下面的代码片段:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [array count] + 1; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    } 

    // Configure the cell. 

    NSUInteger rowN = [indexPath row]; 

    if (rowN == [array count]) { 

     cell.textLabel.text = [NSString stringWithFormat:@"Some Text"]; 

    } else { 

    TFHppleElement* pCell = [array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [pCell content]]; 

    } 

    return cell;  
} 

现在,我收到异常错误:*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]

可能是什么问题?从错误,它看起来像[array objectAtIndex:indexPath.row]正试图访问该数组不存在的第6个索引,但为什么它试图访问第6个索引时,我的代码在else条件?

+0

为什么你还要在 “+1” 只使用indexPath.row代替rowN的你的'numberOfRowsInSection'方法在那里?如果将if(rowN == [array count])更改为if(rowN> = [array count]),会发生什么? –

+1

@MichaelDautermann我相信他想在数组中的行之后添加一行,这就是+ 1 – akoso

+0

@MichaelDautermann相同的结果。 :( –

回答

2

更改该行:

NSUInteger rowN = [indexPath row]; 

到:

NSUInteger rowN = indexPath.row; 

还是在if条件

+0

两种变体都是语义上相当...(当然,你的变体是更干净的,但很可能不会解决实际问题。) – mrueg

+0

啊!实际上找到了解决方案,我没有使用如果条件在'heightForRowAtIndexPath:'方法和'objectAtIndex '试图访问阵列的第6个索引,不好意思打扰他们,因为我在没用的问题上浪费时间投票给你投票 –

+0

@mrueg谢谢指出这一点。那么这不应该是问题,但仍然期待我,问题是如果条件(如果它真的在这个函数中) – akoso