我在可用視圖中有30行,我試圖選擇多行並將它們設置爲檢查。UITableView多選。自動選擇行隨機選擇行
每當我選擇一行時,會自動選擇另一行並檢查所需的行。
另外,當我滾動tableview它會自動改變選擇。
我已經使用此代碼行數較少(8),它的工作完美。對於超過12行,它提供了我所描述的問題。
如果你可以建議任何其他代碼/教程,使其工作也沒關係。
我是xcode的新手,非常感謝任何幫助。謝謝。
這裏是我的代碼:
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [listOfItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
if(count < 3)
{
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
count++;
}
NSLog(@"Count: %d", count);
NSLog(@"Items I selected @ %d:", indexPath.row);
NSLog(@"Items I selected: %@", selectedIndexes);
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
count --;
NSLog(@"Items I de-selected @ %d:", indexPath.row);
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSMutableString *resultlearning = [[NSMutableString alloc]init];
for (int i = 0; i < [listOfItems count]; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
//[tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
[resultlearning appendFormat:@"%@, ",cell.textLabel.text];
}
}
if (resultlearning.length > 2) {
[resultlearning replaceCharactersInRange:NSMakeRange(resultlearning.length-1, 1) withString:@""];
}
NSLog(@"Result: %@",resultlearning);
}
嗨,所以selectedIndexes是你希望我聲明的另一個可變數組,還是應該添加一個比selectedIndexes更多的數組? – 2012-07-25 13:28:15
你不需要添加任何其他可變數組,因爲我假設你有selectedIndexes數組,如果沒有,然後添加可變數組和alloc在viewDidLoad方法 – 2012-07-26 04:36:00