2014-02-21 30 views
-2

每12個格在我的tableview有相同的地址,這會導致一個問題:當我向一個小區,與該地址的所有的細胞得到這裏所謂objective-c。 TableView中的細胞具有相同的地址

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

static NSString *CellIdentifier = @"datacell"; 
DataCell *cell = (DataCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
if (cell==nil) { 
    cell= [[DataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
// Configure the cell... 

if (indexPath.row==1) { 
    cell.backgroundColor= [UIColor redColor]; 
} 
return cell; 

例如,雖然我只將紅色設置爲一個單元格,但每個第13個單元格都會變爲紅色背景。所以我有4個紅色背景的細胞。我不知道發生了什麼:@:@

+1

爲什麼你不知道?這是這裏回答的最常見問題之一。只要搜索,你會發現。 – matt

+0

我確實搜索過,你能幫我一個鏈接嗎? –

+0

例如:http://stackoverflow.com/questions/20781556/strange-behaviour-on-table-view-with-core-data – matt

回答

2

當您滾動時,表格視圖單元格是重用。出於這個原因,你必須 始終設置單元格的屬性,例如:

if (indexPath.row==1) { 
    cell.backgroundColor= [UIColor redColor]; 
} else { 
    cell.backgroundColor= [UIColor clearColor]; 
} 
+0

非常感謝,現在它工作 –