我是iOS開發新手。我不明白tableView的行爲。
爲了測試它,我做了兩個簡單的tableView應用程序,並且在兩種情況下,如果用戶向下滾動直到tableView結束,我已經用99999行加載了帶有標準代碼片段的tableView。cellForRowAtIndexPath:用於StoryBoard和XIB行爲和單元緩存機制?
應用1:TableViewWithStoryBoard的cellForRowAtIndexPath:代碼片段
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // for StoryBoard
if (cell == nil)
{
static int i;
NSLog(@"New Cells created: %d ",++i);
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
return cell;
}
問題1: 如果我刪除
forIndexPath:indexPath from
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // for StoryBoard
則故事板仍樂趣cations在模擬器中正確嗎? 那爲什麼它提供了?
問題2: 爲什麼NSLog(@「New Cells created:%d」,++ i);永遠不會打印,即使我滾動到99999行,這是我的tableView結束?在什麼情況下NSLog會打印一些值?
應用2:TableViewWithNib的cellForRowAtIndexPath:代碼片段
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// for Nib File
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
return cell;
}
問題3: 如果我添加
forIndexPath:indexPath
在XIB版本然後我的應用程序崩潰並引發異常?爲什麼這樣?
異常或錯誤: 終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因:「無法出列具有標識符細胞的細胞 - 必須註冊標識符的筆尖或一類或在一個情節串連圖板連接原型細胞'
代碼有點不清楚,你可以把它放在一起嗎? – jcesarmobile 2013-03-15 12:45:14
@jcesar - 嘿,我已經把所有的代碼放在一起,也提到了有問題的代碼部分。我已經使用StoryBoard和基於SingleView的App2製作了基於單視圖的App1和Nib – Robin 2013-03-15 13:15:36