使用自定义选择“颜色”和“边框”创建分组的UITableviewCell



我目前正在编写一个应用程序,该应用程序在分组的表视图中使用不同的选择颜色。就Borders而言,这非常有效。我可以用这篇文章中的代码更改选择颜色:如何自定义分组表视图单元格的背景/边框颜色?

但是我想在苹果使用的单元格周围再加上一个漂亮的边框。我如何使用此边界?

http://www.mediafire.com/?x2gxbkjqu4d2zto

这是创建背景的代码-大部分复制自以上帖子:http://www.mediafire.com/?kltwlni1mf4t7ks

这就是我使用它的方式:

NSIndexPath * indexPath = [[[NSIndexPath alloc] initWithIndex:0] indexPathByAddingIndex:1];
CGRect frame = [[self tableView] rectForRowAtIndexPath:indexPath];
TLCustomCellBackgroundView * selectedBackgroundView = [[TLCustomCellBackgroundView alloc] initWithFrame:frame andColor:[UIColor redColor]];
[selectedBackgroundView setPosition:CustomCellBackgroundViewPositionBottom];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelectedBackgroundView:selectedBackgroundView];

正如你所看到的,除了第二个单元格周围的灰色边框外,我几乎让它正常工作。

在cellForRowAtIndexPath中,您可以为每一行/节设置边框(分隔符)。

 if(indexPath.row == 1)
    [tableView setSeparatorColor:[UIColor redColor]];
 if(indexPath.section == 2)
    [tableView setSeparatorColor:[UIColor greenColor]];

只需记住为你想要的每一行/每一节设置/重置它。

如果你想在红细胞周围添加灰色边框,那就是

if(indexPath.section == 0 && indexPath.row == 1){
//assuming this is the correct index)  
   [tableView.separatorColor = [UIColor  colorWithRed:0.67 green:0.67 blue:0.67 alpha:1];
}
else
{
   [tableView.separatorColor = [UIColor yourChosenDefaultColor]];
}

实际上,你需要在你的cellForRowAtIndexPath中使用它,否则你将为整个TableView设置边界(也许这就是你想要做的)

我找到了一个解决方案。这并不完全相同(因为分组样式中的UITableviewCell在iOS5中得到了更复杂的设计,但这对我来说已经足够了http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

而且效果很好!

我使用的代码是:http://www.mediafire.com/?mdxkpfe8g74ctk9

希望这能帮助其他

最新更新