无法在tvOS中的自定义表格视图单元格中选择按钮

时间:2017-07-26 11:23:52

标签: objective-c uitableview tvos custom-cell

我正在开发一个使用自定义表格视图单元格的tvOS应用程序,而使用标签显示数据工作得非常好,但按钮选择不起作用。有没有办法在自定义表视图单元格中的按钮选择在tvOS表视图中工作?

代码段

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {
              MsgDetailsTableViewCell *cell;
              static NSString *simpleTableIdentifier = @"Cell";
             cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
              if (cell == nil) {
                    cell = [[MsgDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                }

             NSDictionary*dataDict = [self.qArray objectAtIndex:indexPath.row];
             NSString *followCountText =  [NSString stringWithFormat:@"%@ Following",[[dataDict objectForKey:@"followCount"] stringValue]];
               if([[dataDict objectForKey:@"followCount"] integerValue]!=0)
                {
                    [cell.statusListBtn addTarget:self action:@selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
                    cell.statusListBtn.tag=111;
                    cell.followCountLbl.textColor = [UIColor colorWithRed:0/255.0 green:95/255.0 blue:167/255.0 alpha:1.0];
                }
                else
                {
                    [cell.statusListBtn removeTarget:self action:@selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
                    cell.statusListBtn.tag=111;
                    cell.followCountLbl.textColor = [UIColor grayColor];
                }

                cell.followCountLbl.text=followCountText;
            return cell;
      }

在上面的代码中,statusListBtn的选择无效。

1 个答案:

答案 0 :(得分:0)

你的意思是只有dataDict objectForKey:@"followCount"] integerValue]!=0按钮才能执行选择器。你可以试试这个

              MsgDetailsTableViewCell *cell;
              static NSString *simpleTableIdentifier = @"Cell";
             cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
              if (cell == nil) {
                    cell = [[MsgDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                    [cell.statusListBtn addTarget:self action:@selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
                    cell.statusListBtn.tag=111;
                }

             NSDictionary*dataDict = [self.qArray objectAtIndex:indexPath.row];
             NSString *followCountText =  [NSString stringWithFormat:@"%@ Following",[[dataDict objectForKey:@"followCount"] stringValue]];
               if([[dataDict objectForKey:@"followCount"] integerValue]!=0)
                {
                    cell.statusListBtn.userInteractionEnabled = YES;
                    cell.followCountLbl.textColor = [UIColor colorWithRed:0/255.0 green:95/255.0 blue:167/255.0 alpha:1.0];
                }
                else
                {
                    cell.statusListBtn.userInteractionEnabled = NO;
                    cell.followCountLbl.textColor = [UIColor grayColor];
                }

                cell.followCountLbl.text=followCountText;
            return cell;

如果您想要不同的按钮事件,可以像UIControlEventPrimaryActionTriggered

一样更改UIControlEventTouchUpInside