我正在从事iOS项目,它有一个有两个sections.it的表,还有一个搜索bar.In表单元,我有一个标签和一个button.in,两个部分都来自不同的array.Now数据,问题是在点击button.how时,我无法识别该节的单元格,我可以在自定义按钮操作中识别出哪个按钮是哪个部分?帮助我.I粘贴下面的代码。
- (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];
}发布于 2014-01-03 05:35:29
您需要将行信息和节信息分开发送,以便将标记设置为
Tag= row*1000+section.现在使用row= tag/1000 section =tag%1000从标记获取节和行值
使用此值可以创建正确的索引路径或找到比2分钟的解决方案更好的索引路径。
注意,这是一种解决方法,正确的做法是子类UIButton添加索引路径属性int,它使类生成按钮对象,并嵌入标记集索引路径属性。
在CustomButton.h
@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end在CustomButton.m
@implementation CustomButton
@synthesize path;
@end使用表视图单元格上的自定义按钮,而不是标记设置路径属性。
发布于 2014-01-03 05:36:04
您可以使用按钮检索UITableViewCell,然后获取它的indexPath来标识该部分。如下所示:
UITableViewCell *cell = (UITableViewCell *)[tag superview superview]; int截面= tableView indexPathForCell:cell.section;
在哪里tag是你的按钮,正在被点击。祝好运!
发布于 2014-01-03 05:37:42
尝尝这个
NSString *tempTag=[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
btn.tag=[tempTag intValue];https://stackoverflow.com/questions/20897099
复制相似问题