如您所见,我想要:
1. 减小tableView的宽度(我希望两侧有更多的边距,而分组的tableView提供的边距不够)。 2. 圆角半径(比分组tableView默认值更大)。 3. 在表格周围添加投影,并在最后一个单元格下方添加特殊的阴影。
typedef enum {
CellTypeTop,
CellTypeMiddle,
CellTypeBottom
} cellType;
然后为类型添加一个属性...
@property (nonatomic) cellType cellType
@property UIImageView *bgImageView;
@property UIImage *topImage;
@property UIImage *middleImage;
@property UIImage *bottomImage;
- (void)awakeFromNib //or in the init depends how you are initialising the cell
{
self.bgImageView = [[UIImageView alloc] initWithFrame:blah];
[self.backgroundView addSubView:self.bgImageView];
}
- (void)setCellType:(cellType)cellType
{
switch(cellType) {
case CellTypeTop:
self.bgImageView.image = self.topImage;
break;
case CellTypeMiddle:
self.bgImageView.image = self.middleImage;
break;
case CellTypeBottom:
self.bgImageView.image = self.bottomImage;
break;
}
}
最后,图像的惰性实例化...
- (UIImage *)topImage
{
if (_topImage == nil) {
_topImage = [UIImage imageNamed:@"topImage"];
//alternatively...
_topImage = [[UIImage imageNamed:@"topImage"] stretchableImageWith...
}
return _topImage;
}
::编辑:: 回答迈克尔的问题。
In the storyboard create a UITableViewController (rename the Class in the inspector so that it matches your subclass UITableViewController - I'll call it MyTableViewController).
Create a subclass of UITableViewCell (I'll call mine MyTableViewCell) in the code (i.e. the .h and .m).
Add the above code to do with properties and types and imageViews to your MyTableViewCell.h file.
In the storyboard select the cell in the TableViewController and rename the class to MyTableViewCell. Also set the reuse identifier on it.
In the MyTableViewController code you will need a function like this...
-(UITableViewCell*)tableView:(UITabelView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
MyTableViewCell *cell = [tableView dequeueCellWithReuseIdentifier:@"Cell"];
cell.cellType = CellTypeTop; //or whichever it needs to be
cell.textLabel.text = @"Blah";
return cell;
}
哦,还有一件事,在storyboard中,您将能够布置单元格的外观并链接所有标签和图像视图等... 确保为UIImageView添加IBOutlet,以便在storyboard中链接它。
确保你已经导入了#import <QuartzCore/QuartzCore.h>
,然后你就可以开始访问UITableView的层级了。
UITableView *yourTable = [[UITableView alloc] initWithStyle:UITableViewStyleGrouped];
[[yourTable layer] setCornerRadius:10.0f];
[[yourTable layer] setShadowColor:[[UIColor blackColor] CGColor]];
[[yourTable layer] setShadowOffset:CGSizeMake([CALayer ShadowOffSetWidthWithFloat:10.0f], [CALayer ShadowOffSetWidthWithFloat:10.0f])];
[[yourTable layer] setShadowOpacity:[CALayer ShadowOpacity:1]];
[[yourTable layer] setMasksToBounds:NO];
UIBezierPath *path = [UIBezierPAth bezierPathWithRect:yourTable.bounds];
[[yourTable layer] setShadowPath:[path CGPath]];
这将为您的表视图添加阴影效果,阴影不会被遮罩到UITableView
的边界上,在setCornerRadius
中,您可以将表格的角设置为任何您想要的形状。您还可以通过执行以下操作来设置UITableView
的frame
[yourTable setFrame:CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)];
编辑
如另一位用户试图指出的那样,CALayer
非常慢,这不是事实。
CALayer被引入以帮助处理动画性能问题。请阅读文档。直接加载图像可能看起来是个好主意,但从长远来看会占用更多内存。请参阅有关图像内存分配的问题。正如您所看到的那样,它可能看起来更快,但每个图像将占用2.25兆字节的内存
,在加载每个图像这么多次后,您的应用程序将开始变慢。