我有一個從數組加載的tableview,由於某種原因,它重新使用了單元格,但第二次在表格下方加載了相同的信息。UITableViewCell正在被重用,但我無法解決原因?
http://screencast.com/t/Ig8bcqSpLzp
上面的視頻應該給你明白我的意思的想法。
這是我的代碼加載電池:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
accessicon *current =[arryAccess objectAtIndex:indexPath.row] ;
cell.textLabel.text = current.text;
cell.imageView.image = [UIImage imageNamed: current.iconPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
switchView.myValue = current.iconValue;
if(totalIconValue & current.iconValue) {
[switchView setOn: YES animated:NO];
} else {
[switchView setOn: NO animated:NO];
}
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
return cell;
}
任何幫助將不勝感激
編輯:我已經在實際設備上進行更新。但是我上面的代碼,它仍然選擇項目它不應該 - 不再重複使用名稱,但如果我上下滾動它自己選擇它們?
在這裏看到視頻:http://screencast.com/t/a9N1qbws
湯姆
感謝您花時間爲我解釋,真的很感激。 – TMB87
編輯;我已經上傳了上面的代碼並添加了一個新視頻,它現在正在做其他奇怪的事情!你能看看嗎? – TMB87
這很奇怪,但並不完全不尋常。這表示重新使用的單元沒有正確重新初始化。看看你如何分配交換對象和管理這些對象。您不應該每次都需要分配一個。一旦一個單元格擁有一個,你就不需要總是創建一個單元格。開關創建和目標選擇器設置,以及單元格選擇樣式都可以在塊內完成;這是所有通用的一次性設置。你也可以通過不重複地獲得性能提升。 –