【问题标题】:How to solve border overlapping issue in UITableViewCell?如何解决 UITableViewCell 中的边框重叠问题?
【发布时间】:2013-06-26 15:55:40
【问题描述】:

我正在向 UITableViewCell 添加边框。下面是代码:

Try-1:

cell.layer.borderWidth = 1;
cell.layer.borderColor = [UIColor redColor].CGColor;

Try-2:

cell.contentView.layer.borderWidth = 1;
cell.contentView.layer.borderColor = [UIColor redColor].CGColor;

这两种方法我都试过了,这是输出。

正如您在图像中发现的那样,两个单元格之间的边框重叠,因为这种颜色比边框单元格更深。

为什么我没有添加表格边框?

  • 问题是,我在单个表中使用三个差异自定义单元格,根据数据,正在加载特定单元格
  • 因此我无法为整个 uitableview 添加边框颜色或无法在 cellforrowatindexpath 中绘制单元格边框。因为在该方法中,我只检查它是哪种类型的数据,并相应地加载了自定义单元格。

这是我的cellForRowAtIndexPath

- (UITableViewCell*) getTableViewCellForTraining:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withTrainingInfo:(TrainingInfoiOS*)trainingInfo
{
    BeLearnPlatform& ptr = BeLearnPlatform::GetLearnPlatform();

    static NSString *CustomCellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier forIndexPath:indexPath];
    cell.courseNameLabel.text = trainingInfo.courseName;
    cell.courseStartButton.sectionID = indexPath.section;
    cell.courseStartButton.rowID = indexPath.row;
    cell.tag=0;
 ...
}
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell* cell = nil;
        NSString* dictionaryKey = [self createKeyWithSection:indexPath.section andRow:indexPath.row];
        NSObject* dataObject = [tableViewMappig objectForKey:dictionaryKey];

        if([dataObject isKindOfClass:[MasterCourse class]])
            {
            UITableViewCell* cell = [self getTableViewCellForTraining:tableView cellForRowAtIndexPath:indexPath withTrainingInfo:(MasterCourse*)dataObject];
            return cell;
            }
        else if([dataObject isKindOfClass:[ParentCourse class]])
            {
            UITableViewCell* cell = [self getTableViewCellForParentCourse:tableView cellForRowAtIndexPath:indexPath withTrainingInfo:(ParentCourse*)dataObject];
            return cell;
            }

请建议我解决这个问题。

【问题讨论】:

  • tableViewCell添加分隔符,tableView添加边框层
  • 我做不到。有些单元格的布局与此不同。这些是无边界单元格。所以,我不能在整个表格视图中添加分隔线。

标签: ios uitableview quartz-core


【解决方案1】:

好的,就是这个意思。

您可以这样做,将此代码添加到您创建单元格的cellForRowAtIndexPath 方法中。

如果您想在索引 1,2 和 3 的单元格中应用顺序...

    if (indexPath.row == 1||indexPath.row == 2)
        {

    //Display vertical line on top of the cell
            UIView* vLineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
            vLineview.backgroundColor = [UIColor redColor];
            [cell addSubview:vLineview];
    //Display horizontal line on left of the cell
//44 is default cell size you can change it according to your cell value
    UIView* hLineview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
            hLineview1.backgroundColor = [UIColor redColor];
            [cell addSubview:hLineview1];

    //Display horizontal line on right of the cell
    UIView* hLineview2 = [[UIView alloc] initWithFrame:CGRectMake(319, 0, 1, 44)];
            hLineview2.backgroundColor = [UIColor redColor];
            [cell addSubview:hLineview2];
    }
//Now we give last cell to display vertical line on bottom

if (indexPath.row == 3)
            {

        //Display vertical line on top of the cell
                UIView* vLineview = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
                vLineview.backgroundColor = [UIColor redColor];
                [cell addSubview:vLineview];

        //Display horizontal line on left of the cell
    //44 is default cell size you can change it according to your cell value
        UIView* hLineview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
                hLineview1.backgroundColor = [UIColor redColor];
                [cell addSubview:hLineview1];

        //Display horizontal line on right of the cell
        UIView* hLineview2 = [[UIView alloc] initWithFrame:CGRectMake(319, 0, 1, 44)];
                hLineview2.backgroundColor = [UIColor redColor];
                [cell addSubview:hLineview2];

       //i have added this now
       //Display vertical line on Bottom of the cell
                UIView* vLineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
                vLineview.backgroundColor = [UIColor redColor];
                [cell addSubview:vLineview];

        }

这将在单元格中添加边框,该边框位于 1,2 和 3 位置。 根据您的需要更改视图值。不要将视图设置为底部,否则会出现同样的问题。

【讨论】:

  • 请在我的问题中找到更新。很抱歉没有早点提供这些细节。如果我已经了解数据,那么您的解决方案就是完美的。但是在运行时,我正在检查即将到来的数据类型(因为它来自网络服务)。所以我无法获得下一个单元格中的数据。
  • 忘记重叠边框。告诉我您如何区分哪个 tableCiewCell 将获得边框,哪个不是... bcoz 您的屏幕截图显示您正在添加一些逻辑来区分单元格吗?
  • 通过单元格标识符。首先,我检查它是哪种类型的数据。并相应地传递 diff-diff 标识符。
  • 添加了一种方法供参考.. 与其他具有差异单元标识符的方法相同,并将根据其 UI 返回单元格
  • 您的代码支持很多实现解决方案。谢谢你。
【解决方案2】:
@implementation BaseTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UIView *myBackView = [[UIView alloc] initWithFrame:self.frame];
    self.backgroundColor = [UIColor clearColor];
    myBackView.backgroundColor = [UIColor clearColor];

    self.backgroundView = myBackView;
    self.backgroundView.layer.borderWidth = 2.0;
    self.backgroundView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;

    self.borderTop = [CALayer layer]; self.borderBottom = [CALayer layer];
    self.borderTop.borderColor = self.tintColor.CGColor; self.borderBottom.borderColor = self.tintColor.CGColor;
    self.borderTop.frame = CGRectMake(0, 0, self.frame.size.width, 2);
    self.borderTop.frame = CGRectMake(0, self.bounds.size.height, self.frame.size.width, 2);
    self.borderTop.borderWidth = 2.0; self.borderBottom.borderWidth = 2.0;
    [self.contentView.layer addSublayer:self.borderTop];
    [self.contentView.layer addSublayer:self.borderBottom];
    self.borderTop.hidden = YES; self.borderBottom.hidden = YES;
    self.clipsToBounds = NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    // set selection color
    UIView *myBackView = [[UIView alloc] initWithFrame:self.frame];
    self.selectedBackgroundView = myBackView;
    self.borderTop.hidden = selected ? NO : YES;
    self.borderBottom.hidden = selected ? NO : YES;
    self.clipsToBounds = selected ? NO : YES;

    myBackView.backgroundColor = [UIColor clearColor];
    myBackView.layer.borderColor = selected ?  self.tintColor.CGColor : [UIColor groupTableViewBackgroundColor].CGColor;
    myBackView.layer.borderWidth = 2.0;
}

-(void)layoutSubviews {
    [super layoutSubviews];

    CGRect r;
    UIView *defaultAccessoryView = self.subviews.lastObject;
    r = defaultAccessoryView.frame;
    r.origin.x = self.bounds.size.width - 50;
    r.origin.y = - self.bounds.size.height/2 + 50;
    defaultAccessoryView.frame = r;

    CGRect frameBack = self.bounds;
    frameBack.size.height += 2;
    self.backgroundView.frame = frameBack;
    self.selectedBackgroundView.frame = frameBack;
}

@end

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 2010-10-28
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2010-11-19
    • 2020-05-21
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多