2011-11-27 92 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