为UITableView设置三面边框 - IOS

时间:2013-05-17 04:50:22

标签: iphone ios cocoa-touch uiview uitableview

对于我的设计规格,我的表格视图在顶部,左侧和右侧只应有三个边框。我使用以下代码添加了它....

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

工作正常,但是当我滚动表格时,子视图也会向上移动。我不确定为什么子视图与单元格一起移动而不是它应该与表一起修复,任何想法是什么问题以及如何使用表视图大小来修复它。感谢名单。

1 个答案:

答案 0 :(得分:1)

我不太了解,但我能想到的最简单的方法是在你的桌面视图中添加3个内容视图,如下所示:

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;

}

享受编程!