Ios 如何在tableview内的自定义单元格中标识单击按钮的行索引和节号
您有一些不同的方法可以做到这一点:Ios 如何在tableview内的自定义单元格中标识单击按钮的行索引和节号,ios,objective-c,uitableview,sections,Ios,Objective C,Uitableview,Sections,您有一些不同的方法可以做到这一点: •配置单元格时,将单元格的标记或按钮设置为行,并在iAction上检索该行(尽管我讨厌标记) •为表格单元格定义一个委托,并在配置时进行设置。然后调用传递单元格的委托方法并查找相对索引(-indexPathForCell:) •与之前相同,但通过通知而不是代理传递单元格首先,按钮iAction调用此方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
•配置单元格时,将单元格的
标记或按钮设置为行,并在iAction
上检索该行(尽管我讨厌标记
)
•为表格单元格定义一个委托,并在配置时进行设置。然后调用传递单元格的委托方法并查找相对索引(-indexPathForCell:
)
•与之前相同,但通过通知而不是代理传递单元格首先,按钮iAction调用此方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"CustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: self options: nil ];
cell = [nib objectAtIndex:0];
}
//cell.lblName.text = [items objectAtIndex:indexPath.row];
NSMutableDictionary *dic = [items objectAtIndex:indexPath.row];
cell.imgface.image = [dic objectForKey:@"Image"];
cell.imgface.layer.cornerRadius = cell.imgface.frame.size.width / 2;
cell.imgface.layer.borderWidth = 3.0f;
cell.imgface.layer.borderColor = [UIColor whiteColor].CGColor;
cell.imgface.clipsToBounds = YES;
cell.lblName.text = [dic objectForKey:@"Name"];
[cell.btnPhone setTitle:[dic objectForKey:@"Phone"] forState:(UIControlStateNormal)];
cell.btnPhone.tag = indexPath.row;
cell.btnstar.tag = indexPath.row;
[cell.btnPhone addTarget:self action:@selector(dial:) forControlEvents:(UIControlEventTouchUpInside)];
[cell.btnstar addTarget:self action:@selector(toggleimage:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.section == 0)
{
star = [UIImage imageNamed:@"Star-Favorites.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
else
{
star = [UIImage imageNamed:@"keditbookmarks.png"];
[cell.btnstar setBackgroundImage:star forState:UIControlStateNormal];
}
return cell;
}
cellWithSubview函数
- (IBAction)btnClick:(UIButton)sender
{
UITableViewCell *cellOfcontrol=[self cellWithSubview:sender];
NSIndexPath *indexPath = [self.tableview indexPathForCell:cellOfcontrol];
}
- (UITableViewCell *)cellWithSubview:(UIView *)subview
{
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
write below code in button IBAction and you will get indexpath.row and indexpath.section
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"selected tableview row is %d",indexPath.row);