I am using this code for tableview with checkbox.
- (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];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text =@"a";
int flag = (1 << indexPath.row);
if (_checkboxSelections & flag)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_checkboxSelections ^= (1 << indexPath.row);
[tableView reloadData];
}
- (NSInteger) tableView:(UITableView *)tab开发者_如何学运维leView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
How can i know which cells are selected and which are not when i click on some button?
You can use the following method to access the tableView cells on a button's action. And you can check for the selection using if (cell.accessoryType == UITableViewCellAccessoryCheckmark) condition because you are setting UITableViewCellAccessoryCheckmark for the selected cells.
- (void)onButtonClick {
int numberOfSections = [tableView numberOfSections];
for (int section = 0; section < numberOfSections; section++) {
int numberOfRows = [tableView numberOfRowsInSection:section];
for (int row = 0; row < numberOfRows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
// Cell is selected
} else {
// Cell is not selected
}
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(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] ;
UIButton * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
checkBox.tag=indexPath.row;
checkBox.frame=CGRectMake(270,15, 20, 20);
[cell.contentView addSubview:checkBox];
[checkBox setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
}
cell.textLabel.text=[array objectAtIndex:indexPath.row];
return cell;
}
-(void)checkBoxClicked:(id)sender {
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_not_ticked.png"]])
{
[sender setImage:[UIImage imageNamed: @"checkbox_ticked.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"]forState:UIControlStateNormal];
}
}
For that, you should take one array. And when particular row is selected then you can enter indexpath.row into that array. When the same row is selected, then you can search into array, that row exists and if it is exits then you can remove checkbox from cell and also remove that indexpath.row from array.
So this way you can get all the indexpath.row which are selected.
Hope you got the point.
Let me know in case of any difficulty.
You have to set like this
BOOL selected[array count];
then
set true for selected cell(didselectrow method)
selected[indexPath.row] = !selected[indexPath.row];
in Button click you have to use
NSMutableArray *true = [NSMutableArray array];
for (int i = 0; i < (sizeof(selected) / sizeof(BOOL)); i++) {
if (selected[i]) {
[true addObject:[Yourarray objectAtIndex:i]];
}
}
精彩评论