为UITableView设置三面边框 - IOS
问题描述:
对于我的设计规范,我的表格视图在顶部,左侧和右侧应只有三个边框。我已经添加它使用下面的代码....为UITableView设置三面边框 - IOS
CGSize mainViewSize = menu_table.bounds.size;
CGFloat borderWidth = 1;
UIColor *borderColor = [UIColor lightGrayColor];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, borderWidth, mainViewSize.height)];
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(mainViewSize.width - borderWidth, 0, borderWidth, mainViewSize.height)];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mainViewSize.width, borderWidth)];
leftView.opaque = YES;
rightView.opaque = YES;
topView.opaque = YES;
leftView.backgroundColor = borderColor;
rightView.backgroundColor = borderColor;
topView.backgroundColor = borderColor;
// for bonus points, set the views' autoresizing mask so they'll stay with the edges:
leftView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
rightView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
[menu_table addSubview:leftView];
[menu_table addSubview:rightView];
[menu_table addSubview:topView];
它工作正常,但是当我滚动表,子视图添加也向上移动。我不知道为什么子视图正在移动单元格,而是应该修复表,任何想法是什么问题,以及如何解决与表视图大小。感谢名单。
答
我不知道具体多少,但我能想到的最简单的方法是在你的tableview中添加3个contentviews象下面这样:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
//add left view
UIView *vwLeft=[[UIView alloc] initWithFrame:CGRectMake(X_POS, Y_POS, LINE_WIDTH, CELL_HEIGHT)];
[vwLeft setBackgroundColor:[UIColor redColor]];
[[cell contentView] addSubview:vwLeft];
[vwLeft release];
//add right view
UIView *vwRight=[[UIView alloc] initWithFrame:CGRectMake(X_POS, Y_POS, LINE_WIDTH, CELL_HEIGHT)];
[vwRight setBackgroundColor:[UIColor blueColor]];
[[cell contentView] addSubview:vwRight];
[vwRight release];
//add top view
UIView *vwTop=[[UIView alloc] initWithFrame:CGRectMake(X_POS, Y_POS, LINE_WIDTH, CELL_HEIGHT)];
[vwTop setBackgroundColor:[UIColor yellowColor]];
[[cell contentView] addSubview:vwTop];
[vwTop release];
}
return cell;
}
欣赏节目!
感谢您的建议,我会尽力。 – Newbee
我已经尝试了,现在的问题是我在桌子底部保留了一些图像,如果向上滚动,一旦行向上移动,表格中的空白空间没有边框,并且它没有触摸该图像,因为它具有单元格。看起来奇怪。 – Newbee
你可以附上它的截图! –