【问题标题】:UITableViewCell and disappearing separatorUITableViewCell 和消失的分隔符
【发布时间】:2012-08-15 14:21:26
【问题描述】:

我尝试在 UITableView 中实现自己的简单单元格样式,但分隔符有问题。在正常视图下效果很好,但是当我选择一个单元格时它会消失。我尝试添加到我的 customSelect 视图分隔符,但是我在任何地方都看不到分隔符。如何为选定的单元格添加分隔符?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyCellIdentifier = @"MyCellIdentifier";

    UITableViewCell *cell = [wallMenuTableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];

        MenuItemModel *mItem = [menu.menuPositions objectAtIndex:indexPath.row];
        cell.textLabel.text = mItem.displayName;
        cell.textLabel.textColor = [UIColor colorWithRed:0.70 green:0.70 blue:0.70 alpha:1.0];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:16];
        cell.textLabel.shadowColor = [UIColor blackColor];
        cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0);

        customSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y), 320, 2)];
        customSeparator.backgroundColor=[UIColor blackColor];
        [customSeparator.layer setShadowOffset:CGSizeMake(0.0, 0.8)];
        [customSeparator.layer setShadowOpacity:0.8];
        [customSeparator.layer setShadowRadius:0.8];
        [customSeparator.layer setShadowColor:[UIColor grayColor].CGColor];
        [cell.contentView addSubview:customSeparator];

        customSelect = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y+2), cell.frame.size.width, cell.frame.size.height)];
        //[customSelect addSubview:customSeparator];
        customSelect.backgroundColor = [UIColor clearColor];
        [cell setSelectedBackgroundView:customSelect];

    }

    return cell;
}

目前的结果:

【问题讨论】:

  • 尝试将分隔符创建代码移到 if(cell == nil) 块之外

标签: objective-c ios uitableview


【解决方案1】:

使用 UIImageView 而不是简单的 UIView 作为分隔符。设置 Image 值(这很重要!)而不是 backgroundColor 值并用比例拉伸图像以填充。

【讨论】:

    【解决方案2】:

    可能你的 costumSelect 在 Cell 的 contentView 下。我以前实现过这样的行为,但是我将 UITableViewCell 子类化了。尝试在您的自定义单元格上覆盖 setSelected 方法。

    【讨论】:

    • 我应该只继承 UITableViewCell 还是应该继承 UITableView?
    • 子类化 UITableViewCell。在 -drawRect 方法中,您可以设置视图,在 -setSelected 中,您可以更改处于选中和未选中状态的单元格的行为和界面。 zcentric.com/2008/08/05/custom-uitableviewcell
    【解决方案3】:

    使用 tableView.separatorColor(和 tableView.separatorStyle)设置对比分隔符颜色。如果您在单元格中绘制自己的分隔符,请设置 separatorStyle=UITableViewCellSeparatorStyleNone。此外,将 selectionStyle 设置为 UITableViewCellSelectionStyleNone 可能会对您有所帮助

    【讨论】:

    • 我不想简单地改变颜色或改变标准样式。正如我所说 - 我想创建自己的分隔符和选择样式。
    • 如果您正在绘制自己的分隔符,您应该考虑使用 separatorStyle= UITableViewCellSeparatorStyleNone。此外,将 selectionStyle 设置为 UITableViewCellSelectionStyleNone 可能会对您有所帮助
    猜你喜欢
    • 2013-09-26
    • 2021-12-05
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2013-05-04
    相关资源
    最近更新 更多