iphone - 为 UITableView 设置三边边框 - IOS

标签 iphone ios cocoa-touch uiview uitableview

对于我的设计规范,我的表格 View 应该只有顶部、左侧和右侧三个边框。我已经使用以下代码添加了它......

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];

它工作正常,但是当我滚动表格时,添加的 subview 也向上移动。我不确定为什么 subview 随单元格移动,而是应该用表格修复,知道问题是什么以及如何用表格 View 大小修复它。谢谢。

最佳答案

我不太了解细节,但我能想到的最简单的方法是在你的表格 View 中添加 3 个内容 View ,如下所示:

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;

享受编程的乐趣!

关于iphone - 为 UITableView 设置三边边框 - IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16601614/

相关文章:

ios - Swift:无法使用发件人通过 segue 设置 [String]

ios - 拉动刷新没有 UITableViewController 的 UITableView 不工作

ios - 检测 UIImageView 与任何对象的碰撞

iphone - 在 Linphone iPhone 中调用失败

iphone - 如何在 CAShapeLayer(Rounded-Recr cornered UIView) 上设置 maskToBounds 效果?

ios - CoreData 所需类型 = NSManagedObject;给定类型 = __NSSingleObjectSetI

ios - 如何检查 View Controller 是否添加到堆栈中

iphone - 在没有设计经验的情况下如何更好地掌握 UIKit/Core Animation?

objective-c - 您如何查看或操作您创建的 View 背后的代码?

ios - 快速从 nsdictionary 获取 NSNumber