Iphone 在单击单元格时,我希望提供像general单选按钮这样的功能
我可以使用UITableView中的自定义UIButton选择-取消选择特定行。现在我想让整行在单击时启用选择Iphone 在单击单元格时,我希望提供像general单选按钮这样的功能,iphone,Iphone,我可以使用UITableView中的自定义UIButton选择-取消选择特定行。现在我想让整行在单击时启用选择 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView de
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: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;
}
}
这是根据选择选择单选按钮的代码
-(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];
}
}
简单地说,当我点击特定的单选按钮时,所有的事情都正常工作,但现在我想在点击整个表格单元格的任何位置时执行相同的过程。因此,请帮助我。您可以添加UITableViewDelegate方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self radioBtnPressed:event:];
}
不要忘记tableView.delegate=self 当然,我使用了abstract[self-radioBtnPressed:event:]。你应该调用适当的IBAction radioButtonClick。为什么我使用它,是因为触控事件以指定的值在点击时触发。