On the click of the cell i want to give the functionality like the general radio button
I am able to select - unselect the particular row using the custom UIButton in the UITableView. Now i want to make the whole row enable for the selection on click.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusa开发者_C百科bleCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *rdoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
rdoBtn.frame=CGRectMake(7, 15, 28, 28);
[rdoBtn setBackgroundImage:[UIImage imageNamed:@"radiobut1.png"] forState:UIControlStateNormal];
[rdoBtn setBackgroundImage:[UIImage imageNamed:@"radiobut2.png"] forState:UIControlStateSelected];
rdoBtn.tag=2;
[rdoBtn addTarget:self action:@selector(radioBtnPressed:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rdoBtn];
}
UIButton *btn=(UIButton*)[cell.contentView viewWithTag:2];
if([appDelegate.countryName isEqualToString:[dict objectForKey:@"Country_Name"]])
{
btn.selected = YES;
}
else
{
btn.selected = NO;
}
}
This is the code for the selection of the radio button as per selection.
-(void)radioBtnPressed : (id)sender event : (id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:MyCntryTbl];
NSIndexPath *indexPath = [MyCntryTbl indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
appDelegate.countryName = [[appDelegate.MyCntryArr objectAtIndex:indexPath.row] objectForKey:@"Country_Name"];
appDelegate.countryCode = [[appDelegate.MyCntryArr objectAtIndex:indexPath.row] objectForKey:@"Country_Code"];
[MyCntryTbl reloadData];
}
}
Simply all the things are working proper when i click on particular radio button, but now i want to do same process on the click of any place of the whole cell of the table. So please help me for that.
You can add UITableViewDelegate method :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self radioBtnPressed:event:];
}
and do not forget tableView.delegate = self;
精彩评论