我有一個iPad應用程序(XCode 4.6,ARC,Storyboards,iOS 6.2.3)。我有一個UITableView的UIPopover,它有21行。我可以隨機設置所有行中的accessoryType,但只有在前12行中設置(複選標記)會保留,因此可以在另一個方法中進行檢查並進行處理。我沒有看到前12行和最後9行之間有任何區別。該UITableView的是滾動的,這樣才能到11行後的行,你必須滾動至底部UITableView accessoryType消失在UITableView的後半部分
這裏是代碼設置accessoryType:
#pragma mark didSelectRowAtIndexPath
- (空)的tableView :(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {
// get the cell that was selected
UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
if(theCell.accessoryType != UITableViewCellAccessoryCheckmark)
theCell.accessoryType = UITableViewCellAccessoryCheckmark;
else
theCell.accessoryType = UITableViewCellAccessoryNone;
}
這裏就是我檢查accessoryType並對其進行處理的代碼:
-(void) moveServices { // (moves checked tableViewRows to services tableview)
NSMutableString *result = [NSMutableString string];
for (int i = 0; i < [servicesArray count]; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
[tvServices scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
UITableViewCell *cell = [tvServices cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[result appendFormat:@"%@, ",cell.textLabel.text];
NSLog(@"\n\ni: %d\ncell.accessoryType: %d\ncell.textLabel: %@",i,cell.accessoryType, cell.textLabel);
}
}
if (result.length > 2) { // move to text box in main menu
storeServices =[result substringToIndex:[result length] - 2];
}
}
那麼爲什麼它的前12行,而不是剩餘的行? – SpokaneDude
它只是運氣好。因爲這12行是(當前)可見的行,因此UITableView爲它們創建了單元格。一旦向下滾動得足夠遠,那些「工作」的行的範圍將會改變。這就是爲什麼你不這樣做。保持你的數據源與你的視圖分離,這就是UITableView的設計方式,遵循這一點,一切都會好起來的。 – Bogatyr
好的...我完全理解你在說什麼......但是,我不知道我需要在代碼中更改哪些內容以適應你的建議。你能指點我一個例子還是一個文檔? – SpokaneDude