2011-05-03 53 views
1

我有一個數組,我用於提供表格視圖中的自定義單元格的內容 我不知道什麼是錯的,但是當我滾動tableview的contants oc單元格動態變化iphone:uitableview:滾動內容的單元格更改

請幫我解決這個

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

static NSString *MyIdentifier = @"MyIdentifier"; 
MyIdentifier = @"tblCellView"; 
NSString *offendersImagePath = [self applicationDocumentsDirectory]; 
//NSLog(@"%@", dbPath); 
offendersImagePath=[offendersImagePath stringByAppendingPathComponent:@"Images"]; 


CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if(cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = aCustomCell; 
    aCustomCell=nil; 
} 

NSMutableArray *tempArray;//=[[NSMutableDictionary alloc] init]; 

tempArray=[offendersNamesList objectAtIndex:indexPath.row]; 
//NSLog(@"%d",indexPath.row); 

offendersImagePath=[offendersImagePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[tempArray objectAtIndex:0]]]; 
NSLog(offendersImagePath); 
[[cell offendersImageView] setImage:[UIImage imageWithContentsOfFile:offendersImagePath]]; 
[cell.offendersNameLbl setText:[tempArray objectAtIndex:1]]; 
[cell.offendersViolation setText:[tempArray objectAtIndex:2]]; 
//[tempDictionary release]; 

//[cell setLabelText:[arryData objectAtIndex:indexPath.row]]; 
return cell; 

}

+0

可以從你的cellForRowAtIndexPath – saadnib 2011-05-03 13:52:25

+0

張貼代碼完成提供代碼 – 2011-05-03 13:55:25

回答

1

在這些類型的問題中,通常會遇到的問題是,您沒有在cellForRowAtIndexPath中正確設置單元格中的內容。當一個單元格從屏幕上滾動時,系統將其轉儲到「回收隊列」(我的術語)中。當新的單元格滾動到屏幕上時,系統會在此循環隊列中查找它可以重複使用的單元格。
((CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];)
如果它沒有找到一個,它會繼續前進,並從頭開始建立一個全新的。就你而言,無論出於什麼原因,你都沒有正確設置單元格內容,並且所看到的更改都是未更新正確內容的循環單元格。

我不確定你到底錯在哪裏,但你用於新單元的代碼有點奇怪。它應該看起來更像是這樣的:

if(cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
0

我不是100%,你的問題是什麼,但如果它是具有可變文本長度的項目和對細胞的項目不是活得不會重新調整大小,這是因爲si單元格中包含的標籤的更新沒有更新,只是他們的文本正在改變。

有關在cellForRowAtIndexPath中執行查找的注意事項,或許您應該在viewDidLoad中構造您的數組(對象/字典),以便更高效。

另一件需要注意的是使用.jpg文件。因爲它們在編譯時被壓縮,所以使用.png文件更爲理想。

還使用NSMutableArray * tempArray;和硬編碼索引是非常糟糕的做法,如果某些內容改變了數組中的位置,這意味着您必須更改所有代碼。嘗試使用NSDictionary以便鍵不太可能改變。

相關問題