У меня есть 30 строк в удобном представлении, и я пытаюсь выбрать несколько строк и установить для них флажок.
Всякий раз, когда я выбираю одну строку, автоматически выбирается другая строка и сверяется с нужной строкой.
Также, когда я прокручиваю таблицу, она автоматически меняет выбор.
Я использовал этот код с меньшим количеством строк (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);
}