2014-09-03 38 views
0

我想單擊展開UITableViewCell。嘗試展開UITableView單元時聲明失敗

我之前在應用程序中完成了這項工作,使用與現在使用的步驟相同的步驟,但我想我正在監督某些內容,但我無法弄清楚它是什麼。

我想顯示一些頭條新聞,存儲在一個可變數組

NSMutableArray* topicsArray; 
_topicsArray = [[NSMutableArray alloc]init]; 
[_topicsArray addObject:@"Percent"]; 
[_topicsArray addObject:@"Decimal numbers and fractions"]; 
[_topicsArray addObject:@"Variables and expressions"]; 
[_topicsArray addObject:@"Geometry - Area and perimeter"]; 
[_topicsArray addObject:@"Mathematics in use"]; 
[_topicsArray addObject:@"Reduction of expression"]; 
[_topicsArray addObject:@"Chance and probability"]; 
[_topicsArray addObject:@"Academic reading - Find century"]; 

這是我使用擴大/縮小細胞上點擊代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if ([self.topicsArray containsObject:indexPath]) 
{ 
    [self.topicsArray removeObject:indexPath]; 
} 
else 
{ 
    [self.topicsArray addObject:indexPath]; 

} 
[_tableView beginUpdates]; 
[_tableView endUpdates]; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CGFloat kExpandedCellHeight = 210; 
CGFloat kNormalCellHeigh = 80; 

if ([self.topicsArray containsObject:indexPath]) 
{ 
    return kExpandedCellHeight; 
} 
else 
{ 
    return kNormalCellHeigh; 
} 
} 

當我做了我的自定義單元格,我選擇了「清除圖形內容」和「剪輯子視圖」。

單擊單元格時發生錯誤。的代碼,其中山雀發生 線是

[_tableView endUpdates]; 

- (無效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath方法。

此外,消息我得到的是:

終止應用程序由於未捕獲的異常 「NSInternalInconsistencyException」,理由是:「無效的更新:包含在第0行的數目無效 行數 更新後的現有部分(9)必須等於該部分所包含的行數 更新前(8),加號或減號 插入或刪除該行的行數(0插入, 0刪除)並加上或減去移入或移出的行數 該部分(0移入,0移出)。'

問題的根源是什麼?

回答

0

我真的想出瞭如何解決這個問題。

將所有對象添加到數組後,您將從中填充tableView中的數據,並將該數組保存爲常量。

@property int counter; 

_counter = _array.count; 

然後,在numberOfRowsInSection,傳遞計數器:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _counter; 
} 

瞧,問題就迎刃而解了。

相關問題