hai,I want to place the checkbox in every row of tableview.Please provide any example code for place the checkbox in tableview cells
you can do something like this
- (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];
UIButton * btnShare=[[UIButton alloc] initWithFrame:CGRectMake(270,0,32,32)];
[btnShare setImage:[UIImage imageNamed:@"check-box.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:@"check-box-selected.png"] forState:UIControlStateSelected];
[btnShare addTarget:self action:@selector(accessoryButtonTapped:withEvent:) forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = btnShare;
[btnShare release];
}
BOOL isShared = ([arrayOfSelectedIndex indexOfObject:[NSNumber numberWithInt:indexPath.row]] != NSNotFound);
UIButton* btnShare1 = (UIButton*)cell.accessoryView;
btnShare1.selected = isShared;
[btnShare1 setNeedsDisplay];
return cell;
}
where arrayOfSelectedIndex is an array containing all the indexes which are selected.
There are few more methods aiding the multi selection of rows but I am sure the above snippet can give you some idea as to how to get check boxes in cells.
Hope it helps..Cheers
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[tableView reloadData];
}
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{
NSIndexPath * indexPath = [neighbourTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: neighbourTableView]];
if ( indexPath == nil )
return;
[arrayOfSelectedIndex removeAllObjects];
[arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
[self tableView:neighbourTableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[arrayOfSelectedIndex removeAllObjects];
[arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
[tableView reloadData];
}
Use UIButton and set it's default and selected image as unchecked and then checked.
Here is the tutorial, and there are many similar questions related to this on stack,before putting same question again, try to search it.
精彩评论