我现在遇到问题几天了。问题是我有一个包含多个自定义表格单元格的表格,每个单元格都有多个按钮。但我总是将标签值设为零。以下是我的代码
- (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);
}
标记值始终返回零..在静态和动态按钮操作中。
答案 0 :(得分:3)
尝试这种方法,更清晰地查找视图的封闭行。
-(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);
}
答案 1 :(得分:2)
我尝试了一个示例项目,它对我来说很合适
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = (UITableViewCell*)[array objectAtIndex:0];
}
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);
}
并点击按钮点击我的日志显示
2013-07-23 11:58:31.719 Trial[895:11303] Tag Value = 0
2013-07-23 11:58:33.726 Trial[895:11303] Tag Value = 1
2013-07-23 11:58:35.126 Trial[895:11303] Tag Value = 2
2013-07-23 11:58:35.998 Trial[895:11303] Tag Value = 3
2013-07-23 11:58:36.910 Trial[895:11303] Tag Value = 4
答案 2 :(得分:1)
尝试设置带有剖面的标签,如下图
cell.M_CtrlBtnChat.tag = [[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]intValue];
如果你有超过1个部分,并且在每个部分你有1行,那么你可能得到所有行的0,所以得到部分的值也像上面的代码..
对于EX:如果您希望将行号作为标记添加到按钮,那么您已使用indexPath.row
分配了该行号,并且每次都会获得0
但如果您设置了00
它与部分然后你得到它像10
,20
,dequeueReusableCellWithIdentifier
等。如果你有一个多个部分,在每个部分你只有一行。强>
更新:现在也将NSString *cellID = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row];
CuatomCell_Contacts *cell = (CuatomCell_Contacts *) [tableView dequeueReusableCellWithIdentifier:cellID];
设置为不同的值并将其检出。像吼叫......
{{1}}
答案 3 :(得分:-3)
您以错误的方式初始化了一个按钮
相反,您的代码如下,
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];
请尝试使用以下代码
UIButton *button = [UIButton alloc]init];
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];
享受编程!