Objective c 在iOS 6中实现iOS 7 UITableViewCell分组样式
我正在尝试在iOS 6中“扁平化”我的应用程序,并实现与iOS 7 UITableView分组相同的外观,因此我将UITableViewCell子类化并应用以下代码:Objective c 在iOS 6中实现iOS 7 UITableViewCell分组样式,objective-c,uitableview,Objective C,Uitableview,我正在尝试在iOS 6中“扁平化”我的应用程序,并实现与iOS 7 UITableView分组相同的外观,因此我将UITableViewCell子类化并应用以下代码: -(void)setFrame:(CGRect)frame{ if (self.superview) { if (!IS_IOS7){ float cellWidth; if (IS_IPAD){ cellWidth = 87
-(void)setFrame:(CGRect)frame{
if (self.superview) {
if (!IS_IOS7){
float cellWidth;
if (IS_IPAD){
cellWidth = 870;
}
else{
cellWidth = 350;
}
frame.origin.x = (self.superview.frame.size.width - cellWidth) / 2;
frame.size.width = cellWidth;
}
}
[super setFrame:frame];
}
它工作正常,但我必须在cellForRow方法中手动调整单元格的插入
我尝试将表格设置为普通,删除空单元格,但标题没有固定,因此我尝试将其设置为tableHeaderView,但由于搜索栏的原因,无法按预期工作
那么,有没有更好更干净的方法来实现我所错过的呢 下面介绍如何使iTableView在iOS 6和iOS 7上看起来与iOS 7分组样式一样。这是一种非常手动的方法,但看起来很合适 1) 创建UITableViewCell子类。在标头中,定义枚举以处理分组位置的类型。加上设置值的方法
- (void)setGroupedStyle:(MMTableViewCellGroupStyle)groupedStyle;
typedef NS_ENUM(NSUInteger, MMTableViewCellGroupStyle) {
MMTableViewCellGroupStyleSingle,
MMTableViewCellGroupStyleTop,
MMTableViewCellGroupStyleBottom,
MMTableViewCellGroupStyleMiddle
};
2) 在UITableViewCell子类中,根据单元格的分组样式为其创建UIView。您需要创建一个自定义UIView类,该类在drawRect:
中绘制1像素线,根据线的位置将颜色与顶部或底部对齐。您还需要手动缩进中间单元格底部的行
我很快就完成了,所以它不是最干净最枯燥的代码,但只是为了让你有一个想法。还要注意的是,我没有创建UIView子类来为视网膜设备绘制1像素线,这只是创建了具有1点线的视图。留给读者的练习=)
3) 在UITableViewDataSource
中配置单元格时,为每个单元格设置适当的groupStyle
MMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
[cell setGroupedStyle:MMTableViewCellGroupStyleSingle];
} else if (indexPath.row == 0) {
[cell setGroupedStyle:MMTableViewCellGroupStyleTop];
} else if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
[cell setGroupedStyle:MMTableViewCellGroupStyleBottom];
} else {
[cell setGroupedStyle:MMTableViewCellGroupStyleMiddle];
}
4) 在UITableViewDataSource
中,为节脚返回一个合适的高度。这将是各部分之间的空白。实现返回页脚视图的方法以返回空UIView
5) 确保为每个分组的分区使用分区。在最顶部添加一个额外的部分,因为您需要额外的空间来引导到第一个部分。只是为了确保我理解您想要实现的外观。。。这有点像iOS 7上的设置应用是吗?各部分之间有空间,细线在各部分的顶部/底部全宽,行与行之间有部分线,或者类似的东西?是的,完全一样。唯一的区别是自定义标题,我已经有了。
MMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if ([tableView numberOfRowsInSection:indexPath.section] == 1) {
[cell setGroupedStyle:MMTableViewCellGroupStyleSingle];
} else if (indexPath.row == 0) {
[cell setGroupedStyle:MMTableViewCellGroupStyleTop];
} else if (indexPath.row == ([tableView numberOfRowsInSection:indexPath.section] - 1)) {
[cell setGroupedStyle:MMTableViewCellGroupStyleBottom];
} else {
[cell setGroupedStyle:MMTableViewCellGroupStyleMiddle];
}