我正在为UITableViewCell添加边框。以下是代码:
尝试-1:
cell.layer.borderWidth = 1;
cell.layer.borderColor = [UIColor redColor].CGColor;
尝试-2:
cell.contentView.layer.borderWidth = 1;
cell.contentView.layer.borderColor = [UIColor redColor].CGColor;
我试过这两种方式,这是输出。
正如你在图像中找到的那样,边界在两个单元格之间重叠,因为这种颜色比边框单元格更暗。
为什么我没有添加表格边框?
这是我的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;
}
请建议我解决此问题。
答案 0 :(得分: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位置的单元格中添加边框。 根据需要更改视图值。不要将视图设置为底部,否则会出现同样的问题。
答案 1 :(得分: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