gpt4 book ai didi

ios - 关于 TableView 中按钮操作的识别部分的问题

转载 作者:行者123 更新时间:2023-11-28 22:19:03 24 4
gpt4 key购买 nike

我正在开发 iOS 项目,该项目有一个包含 2 个部分的表格。它也有搜索栏。在表格单元格中我有一个标签和一个按钮。在两个部分中来自不同数组的数据。现在的问题是我不是能够在点击按钮时识别该部分的单元格。我如何在该自定义按钮操作上识别按钮的哪个部分? p;请帮助我。我正在粘贴以下代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else
{
UIView *subview;
while ((subview= [[[cell contentView]subviews]lastObject])!=nil)
[subview removeFromSuperview];

}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,12,230,40)];
[cell.contentView addSubview:nameLabel];
//lblCal.backgroundColor=[UIColor blueColor];//[UIColor colorWithRed:190 green:0 blue:0 alpha:1];
nameLabel.textColor=[UIColor blackColor];
nameLabel.font=[UIFont fontWithName:@"Helvetica" size:18.0];//[UIFont fontWithName:@"Helvetica" size:12.0];
nameLabel.textAlignment=NSTextAlignmentLeft;
nameLabel.backgroundColor=[UIColor clearColor];

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[cell.contentView addSubview:btn];
btn.frame=CGRectMake(255,0,65, 65);
btn.tag=indexPath.row;
[btn addTarget:self action:@selector(sendFollowUnfollowRequest:) forControlEvents:UIControlEventTouchUpInside];
Team* team;
if(isFiltered)
{
if (indexPath.section==0) {

team = [filterFollow objectAtIndex:indexPath.row];
[btn setImage:[UIImage imageNamed:@"icon_check@2x.png"] forState:UIControlStateNormal];
}
if(indexPath.section==1)
{
team = [filterUnFollow objectAtIndex:indexPath.row];
[btn setImage:[UIImage imageNamed:@"icon_plus@2x.png"] forState:UIControlStateNormal];

}
}
else
{
if (indexPath.section==0) {
team = [followTable objectAtIndex:indexPath.row];
[btn setImage:[UIImage imageNamed:@"icon_check@2x"] forState:UIControlStateNormal];

}
if(indexPath.section==1)
{
team = [unfollowTable objectAtIndex:indexPath.row];
[btn setImage:[UIImage imageNamed:@"icon_plus@2x.png"] forState:UIControlStateNormal];
}
}
[nameLabel setText:team.teamName];
return cell;
}

-(void)sendFollowUnfollowRequest:(UIButton *)tag
{
[self.searchBar resignFirstResponder];
}

最佳答案

您需要分别发送行和节信息,因此将标记设为

Tag= row*1000+section.

现在使用 row= tag/1000 section =tag%1000 从标签获取部分和行值

使用这个值来制作正确的索引路径或找到一些比 2 分钟更好的解决方案来获得它。

请注意,正确的解决方法是 子类 UIButton 在其中添加一个 indexpath 属性 让你的类制作按钮对象并插入标签集索引路径属性。

在 CustomButton.h 中

@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end

在 CustomButton.m 中

@implementation CustomButton
@synthesize path;
@end

在您的表格 View 单元格上使用此自定义按钮,而不是标记设置路径属性

关于ios - 关于 TableView 中按钮操作的识别部分的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897099/

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