【问题标题】:UITableViewCell. How do I just highlight cell during touch down?UITableViewCell。如何在触地期间突出显示单元格?
【发布时间】:2012-05-01 17:54:27
【问题描述】:

我有一个自定义的 tableViewCell。我想通过突出显示来指示用户触地。单元格选择样式为UITableViewCellSelectionStyleBlue。在父控制器中我设置了self.clearsSelectionOnViewWillAppear = YES

我应该可以走了。没有。选择仍然粘在单元格上。我想要的是仅在触地期间的选择指示。外观应在修饰时立即恢复为未选择的外观。

我该怎么做?

干杯,
道格

【问题讨论】:

    标签: ios uitableview custom-cell


    【解决方案1】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    

    【讨论】:

    • 我们赢了!对于在家的人来说,实际的方法签名是 deselectRowAtIndexPath:animated:。甜的。干杯。
    • 我不敢相信我花了这么多钱来解决这个疼痛问题......你只是认为 cocoa touch 会在 didHighlightItemAtIndexPath 做到这一点,谢谢@Dancreek
    【解决方案2】:

    以下 Swift 示例使用didHighlightRowAtIndexPath 更改触摸时单元格的背景颜色,并使用didUnhighlightRowAtIndexPath 重置颜色 - 见下文:

    // MARK: UITableViewDelegate
    
    func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
      if let cell = tableView.cellForRowAtIndexPath(indexPath) {
         cell.backgroundColor = UIColor.greenColor()
      }
    }
    
    func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
      if let cell = tableView.cellForRowAtIndexPath(indexPath) {
         cell.backgroundColor = UIColor.blackColor()
      }
    }
    

    【讨论】:

    • 这不起作用。颜色不会在修饰时重置
    • 你能在didUnhighlightRowAtIndexPath: 中设置一个断点并确认它在修改时被调用吗?如果你能确认这一点,那么下一个罪魁祸首可能是你的变色逻辑。
    • 抱歉,我意识到我的问题是我在情节提要中选择了“延迟内容触摸”。
    【解决方案3】:

    覆盖- (void)setSelected:(BOOL)selected animate:(BOOL)animated而不调用super

    【讨论】:

    • 只有在触摸后才会选择单元格 - 问题要在触摸时更改颜色。
    • 覆盖 - (void)setHighlighted:(BOOL)highlighted animate:(BOOL)animated without call super
    【解决方案4】:

    这行得通:

    它获得的单元格边框看起来像分隔符的背景视图。不要更改界面生成器中的默认表格视图设置。确保 UITableViewCellSelectionStyleNone 未设置为 selectionstyle。我正在粘贴工作代码。 :

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *kCellIdentifier = @"PlayListCell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    }
    MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row];
    cell.textLabel.text = playList.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]];
    MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ];
    
    cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]];
    
    // the main code which make it highlight
    
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f];
    [bgColorView.layer setBorderColor:[UIColor blackColor].CGColor];
    [bgColorView.layer setBorderWidth:1.0f];
    [cell setSelectedBackgroundView:bgColorView];
    
    
    return cell;
    

    }

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。下面的代码非常适合我。

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      [tableView deselectRowAtIndexPath:indexPath animated:YES];
      }
      

      【讨论】:

      • 您真的想在每次触摸事件时重新加载表格吗?做主题的方式绝对是错误的。
      • 这会将单元格变为蓝色,但它会保持这种状态,并且仅适用于 touchUpInside。只着陆呢?
      • 该单元格仅在触摸后标记为选中 - 问题要在触摸时更改颜色;另外,我同意@surfrider,你为什么要重新加载数据?
      猜你喜欢
      • 2014-08-02
      • 2013-07-09
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多