Saya memiliki 30 baris dalam tampilan yang sesuai dan saya mencoba memilih beberapa baris dan mengaturnya untuk dicentang.
Setiap kali saya memilih satu baris, secara otomatis baris lain dipilih dan dicentang dengan baris yang diinginkan.
Juga ketika saya menggulir tampilan tabel, itu mengubah pilihan secara otomatis.
Saya telah menggunakan kode ini dengan jumlah baris lebih sedikit (8) dan berfungsi dengan baik. Untuk lebih dari 12 baris itu memberikan masalah yang telah saya jelaskan.
Jika Anda dapat menyarankan kode/tutorial lain untuk membuatnya berfungsi juga tidak masalah.
Saya baru mengenal xcode, bantuan apa pun sangat kami hargai. Terima kasih.
Ini kode saya:
#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);
}