gpt4 book ai didi

ios - 自定义单元格按钮标签始终返回零

转载 作者:行者123 更新时间:2023-12-01 19:09:09 25 4
gpt4 key购买 nike

几天来我遇到了一个问题。问题是我有一个包含多个自定义表格单元格的表格,每个单元格都有多个按钮。但我总是将标签值设为零。下面是我的代码

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CuatomCell_Contacts *cell = (CuatomCell_Contacts *) [tableView dequeueReusableCellWithIdentifier:@"cellA"];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CuatomCell_Contacts" owner:Nil options:nil];

for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CuatomCell_Contacts *) currentObject;

break;
}
}
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
IstructContacts *ObjIstructContacts = nil;
if (table == self.searchDisplayController.searchResultsTableView)
{
ObjIstructContacts = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
ObjIstructContacts = [self.sectionedListContent objectAtIndexPath:indexPath];
}

cell.m_CtrlLblName.text = ObjIstructContacts.m_strName;
//cell.m_CtrlLblContact_Id.text=ObjIstructContacts.m_strContact_Id;
NSLog(@"%@",ObjIstructContacts.m_strImageUrl);
if ([ObjIstructContacts.m_strImageUrl isEqualToString:@"No_image"])
{
cell.m_CtrlImgImage.image=[UIImage imageNamed:@"Person_Dumy.jpeg"];
}
else
{
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ObjIstructContacts.m_strImageUrl]];

cell.m_CtrlImgImage.image=[UIImage imageWithData: imageData];
}
cell.M_CtrlBtnChat.tag=indexPath.row;
[cell.M_CtrlBtnChat addTarget:self action:@selector(MoveToNextView:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(MoveToNextView:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Custom Action" forState:UIControlStateNormal];
button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
button.tag=indexPath.row;
[cell.contentView addSubview:button];

return cell;
}



-(void)MoveToNextView:(id)sender
{
UIButton *button = (UIButton *)sender;

NSLog(@"Tag Value = %d",button.tag);
}

标记值始终返回零。在静态和动态按钮操作中。

最佳答案

试试这个方法,更简洁的方法来查找 View 的封闭行。

 -(IBAction)cellButtonTapped:(id)sender {
UIButton *button = sender;
CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.tableview];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
NSLog(@"Button tapped in row %d",indexPath.row);
}

关于ios - 自定义单元格按钮标签始终返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17802564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com