我知道这里有很多关于这个问题的重复项,但我的要求是我在一个单元格上添加了 2 个 UIButtons,并且这两个按钮将打开两个不同的视图。如果我将属性 userInteractionEnabled 设置为 YES,那么它不会从下面的代码中获取 didSelectRowAtIndexPath 中的“finalID”。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableViewProject){
static NSString *cellId = @"attachmentCellId";
attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"attachmentCell" owner:self options:Nil];
for(id object in nib)
{
if([object isKindOfClass:[attachmentCell class]])
{
cell = (attachmentCell *)object;
break;
}
}
UIButton *button;
button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
button.userInteractionEnabled = YES;
//button.userInteractionEnabled = NO;
[cell.contentView addSubview:button];
UIButton *buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
[buttonAttach addTarget:self action:@selector(buttonAttachClicked) forControlEvents:UIControlEventTouchUpInside];
buttonAttach.userInteractionEnabled = YES;
//buttonAttach.userInteractionEnabled = NO;
[cell.contentView addSubview:buttonAttach];
cell = [nib objectAtIndex:0];
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:@"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:@"%d", attach.attachment];
cell.date.text = attach.date;
}
return cell;
}
我的 DidSelectRowAtIndexPath 是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Array == %@", anotherTempArray);
NSString *finalId = [NSString stringWithFormat:@"%@", [anotherTempArray objectAtIndex:indexPath.row]];
NSLog(@"final id for selected row = %@", finalId);
NSUserDefaults *defaultForFinalId = [NSUserDefaults standardUserDefaults];
NSString *setFinalId = finalId;
[defaultForFinalId setObject:setFinalId forKey:@"SETFINALID"];
if(tableView == self.tableViewProject)
{
[self buttonClicked];
//[self viewDidLoadForList];
}
if(tableView == self.tableViewAttachmentList)
{
[self buttonAttachClicked];
}
}
如果你想调用选择器UIButton
在细胞内部,那么你不需要使用什么didSelectRowAtIndexPath
方法。
在添加处理程序之前,您所做的都是正确的UIButton
。现在,删除您的didSelectRowAtIndexPath
按钮的点击处理程序的代码。以下是如何从按钮单击处理程序获取indexPath。
- (void)buttonClicked:(UIButton *)sender {
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview; //Since you are adding to cell.contentView, navigate two levels to get cell object
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Now you can do the code you put in didSelectRow here.
}
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)