I have designed a Table view , For Select Followers list to send message,
so there I can select some followers and then send them message on twitter.
I am using UITable View with UITableViewCellAccessoryCheckmark to make
this.

But when I scroll down table, the checked rows get unchecked every
time.
I have decleard table view and an array for selected followers in FollowersListView.h
@interface FollowersListView : UIViewController<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *tableFollowers;
NSMutableArray *listFollowrsName;
NSMutableArray *listSelectedFollowrsId;
}
@property(nonatomic,retain) NSMutableArray *listFollowrsName;
@property(nonatomic,retain) NSMutableArray *listSelectedFollowrsId;
@end
and my tableview code is like this in FollowersListView.m file
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listFollowrsName count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"];
//if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:14];
//cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryType = UITableViewCellStyleDefault;
//}
NSString *cellValue = [listFollowrsName objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
//to select a row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(@"row is un-checked %@",[NSNumber numberWithInt:path.row]);
[listSelectedFollowrsId removeObject:[NSNumber numberWithInt:path.row]];
NSLog(@"Now selected person are%@",listSelectedFollowrsId);
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"row is checked %@",[NSNumber numberWithInt:path.row]);
[listSelectedFollowrsId addObject:[NSNumber numberWithInt:path.row]];
NSLog(@"Now selected person are%@",listSelectedFollowrsId);
}
}
I guess I need to load every cell as per the already stored selected list but not getting how to do.
Can anyone guide where I’m doing wrong,
Any help would be appreciated
Edit
@Novarg
I have declared “listSelectedFollowrsId” as a NSMutable Array in .h file and initialized in viewDidLoad method then adding/removing values in didSelectRowAtIndexPath method to manage selected rows.
The thing is that you create a new cell every time that you scroll. The
if (cell == nil)condition must be there, otherwise you will just keep on re-writing the cell over and over again.Try the following:
It might not work, I’m using a windows machine right now, so can’t compile the code and test it myself now.
Hope it helps
EDIT
If it doesn’t solve the problem I’d also add a variable(probably an
NSMutableDictionary) to keep a record of the names that have been “checked”. Using that you can add the following just beforereturn cell;incellForRowAtIndexPath:method:EDIT 2
Here’s what you should add right before
return cell;(didn’t see thelistSelectedFollowrsIdarray before):That will set the accessory to checkmark if the row has been previously tapped.