首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表视图中按钮动作识别部分的问题

表视图中按钮动作识别部分的问题
EN

Stack Overflow用户
提问于 2014-01-03 05:23:37
回答 4查看 484关注 0票数 0

我正在从事iOS项目,它有一个有两个sections.it的表,还有一个搜索bar.In表单元,我有一个标签和一个button.in,两个部分都来自不同的array.Now数据,问题是在点击button.how时,我无法识别该节的单元格,我可以在自定义按钮操作中识别出哪个按钮是哪个部分?帮助我.I粘贴下面的代码。

代码语言:javascript
复制
- (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];
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-01-03 05:35:29

您需要将行信息和节信息分开发送,以便将标记设置为

代码语言:javascript
复制
Tag= row*1000+section.

现在使用row= tag/1000 section =tag%1000从标记获取节和行值

使用此值可以创建正确的索引路径或找到比2分钟的解决方案更好的索引路径。

注意,这是一种解决方法,正确的做法是子类UIButton添加索引路径属性int,它使类生成按钮对象,并嵌入标记集索引路径属性。

在CustomButton.h

代码语言:javascript
复制
@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end

在CustomButton.m

代码语言:javascript
复制
@implementation CustomButton
@synthesize path;
@end

使用表视图单元格上的自定义按钮,而不是标记设置路径属性。

票数 2
EN

Stack Overflow用户

发布于 2014-01-03 05:36:04

您可以使用按钮检索UITableViewCell,然后获取它的indexPath来标识该部分。如下所示:

UITableViewCell *cell = (UITableViewCell *)[tag superview superview]; int截面= tableView indexPathForCell:cell.section;

在哪里tag是你的按钮,正在被点击。祝好运!

票数 0
EN

Stack Overflow用户

发布于 2014-01-03 05:37:42

尝尝这个

代码语言:javascript
复制
 NSString *tempTag=[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
 btn.tag=[tempTag intValue];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20897099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档