首页 > tableviewcell上的button不响应点击事件

tableviewcell上的button不响应点击事件

问题描述:自定义了一个tableviewcell,上面有一个button,添加了一个单击事件。tableviewcell是用xib做的,点击响应在cell的类里实现。现在单击button没有响应。
疑惑:应该跟xcode和ios版本没有关系。我是从另一个工程直接拷贝来的,另一个工程里可以响应,这两个工程的区别是arc和mrc,mrc的有响应,arc的没响应,不知道有没有关系。我换了另一种方法——在viewcontrol里处理cell,在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里给button添加事件并在该viewcontrol里处理事件,可还有没有响应,只能响应cell的点击事件。

请高手指教。
- (IBAction)actionJiaohao:(id)sender
{
NSString *selectType = nil;

if (self.oyised.length)
{
    [self.jiaohao setBackgroundImage:[UIImage imageNamed:@"jiahao.png"]
                            forState:UIControlStateNormal];
    selectType = @"-1";
    self.oyised = nil;
}
else
{
    [self.jiaohao setBackgroundImage:[UIImage imageNamed:@"jianhao.png"]
                            forState:UIControlStateNormal];
    selectType = @"1";
    self.oyised = selectType;
}
OYIDefaultStore *oyids =
[[OYIDefaultStore alloc] init];
user_ID = [oyids OyiRead:@"userid"];
creator_Name = [oyids OyiRead:@"name"];

[self createActivityView:self.superview];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //发送信息到服务器
    post *publish = [[post alloc] init];
    [publish OyiPostPl:user_ID
                 keyID:selectType
               Creator:creator_Name
                  QQID:self.QQID
                  HTID:nil
                 title:nil
                  text:nil
                 image:nil
                 phone:nil
              password:nil
              username:nil
                   url:@"aplMyAttentionUpdate"];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self stopActivityView];
    });
});

}


这篇文章写得很详细:http://blog.csdn.net/u011619283/article/details/43057941


说一下我的两张解决办法。

第一种是我以前的做法,你在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

添加

[cell.testBtn addTarget:self action:@selector(Btnaction:) forControlEvents:UIControlEventTouchUpInside];
cell.testBtn.tag = indexPath.row;

然后实现

- (void)addReply:(UIButton *)sender
{
    NSUInteger tt = sender.tag;//确定是哪一个cell的事件
}

这是一种在viewcontroller解决确定cell中button的方式。

不推荐这种,这种方式会导致viewcontroller中代码过多。

第二种方式。利用代理或者回调或者通知。
手上暂时没有相关的代码我就不贴了。说一下思路。就是在cell里使用代理实现在viewcontroller的方法。

【热门文章】
【热门文章】