1

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

试一试:

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

尝试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;
            }

请为此建议我解决方案。

4

2 回答 2

3

好的,这就是想法。

您可以这样做,在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 位置的单元格中添加边框。根据您的需要更改视图值。不要将视图设置为底部,否则会出现同样的问题。

于 2013-06-26T16:08:14.437 回答
0
@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
于 2017-04-02T13:17:23.920 回答