Reputation: 225
I have 30 rows in uitable view and I am attempting to select multiple rows and set them to checked.
Whenever I select one row, automatically another row is selected and checked with the desired row.
Also when I scroll the tableview it changes selection automatically.
I had used this code with less number of rows (8) and it worked perfectly. For more than 12 rows it gives the problem I have described.
If you can suggest any other code/tutorial to make it work is also okay.
I am new to xcode, any help is much appreciated. Thank you.
Here is my code:
#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);
}
Upvotes: 1
Views: 2645
Reputation: 38259
Add one mutablearray in .h file.Here add as many object to array as your tableDataSourc array has. Now my intension is that if it contains 0 then that cell is not selected or it contains 1 then that cell is selected
Now do this as u have selectedIndexes array so need of another mutablearray :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
for(int i = 0;i<[listOfItems count];i++)
{
[selectedIndexes addObject:@"0"];
}
return [listOfItems count]
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"1"];
}
else
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[selectedIndexes replaceObjectAtIndex:indexPath.row withObject:@"0"];
}
Now selectedIndexes has object with 1 value means that value is selected in listOfItems
Upvotes: 1
Reputation: 17186
The logic of check/uncheck accessory type of cell should go to cellForRowAtindexPath. In didSelectRowAtIndexPath, you should only take care of selected row.
Upvotes: 2
Reputation: 31026
Cells are cached and reused so if you set the accessory on one and then it's used for a different location in the table, it still has the accessory.
It's better to only keep track of which cells are selected as data in the didSelectRowAtIndexPath:
method and then turn the accessory on or off for all cells in cellForRowAtIndexPath:
.
Upvotes: 2
Reputation: 728
Hmm...I'm still on my first cup of coffee this morning, but something tells me you already have the row "selected" when the didSelectRowAtIndexPath is called. Then it seems as though the if statement may be doing something funky and selecting the next row or something. Have you tried commenting out the if statement? I know that probably will only have one selected at a time, but just to see if that's the cause. Also, is it the next row which is selected, the one before?
Upvotes: 0