我想实现这样的目标
主要用于说明目的,是否有我不知道的 UITableView 方法,或者有某种技巧。
这是写在 UItableView 的 Header 和 Footer 中的。所以自定义表中section的headerView和footerView。
使用这些方法:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
希望它可以帮助你。
您在我的应用程序中遵循它对我有用的方法:
- (void)viewDidLoad {
[super viewDidLoad];
/* ios 7 Change */
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.edgesForExtendedLayout = UIRectEdgeNone;
self.tableview.separatorInset = UIEdgeInsetsZero;
[self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *header;
float start = 10.0, startLabel = 10.0;
if ([UICommonUtils isiPad])
{
startLabel = 50.0;
} else {
startLabel = 10.0;
}
CGRect rectFrame = [UIScreen mainScreen].applicationFrame;
UIView* customView = [[[UIView alloc]
initWithFrame:CGRectMake(start, 0.0, rectFrame.size.width - 20.0, 80.0)]
autorelease];
/*
customView.backgroundColor =
[UIColor colorWithRed:.6 green:.6 blue:1 alpha:.9];
*/
UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
//UILabel * headerLabel = [[[UILabel alloc] initWithStyle:UITableViewCellStyleDefault] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
//headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.textColor = [UIColor colorWithRed:0.298039 green:0.337255 blue:0.423529 alpha:1];
headerLabel.shadowColor = [UIColor whiteColor];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
headerLabel.font = [UIFont boldSystemFontOfSize:17.0];
//headerLabel.frame = CGRectMake(startLabel, 0, rectFrame.size.width - 60.0, 20.0);
headerLabel.frame = CGRectMake(startLabel, 10, rectFrame.size.width - 35.0, 20.0);
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.lineBreakMode = UILineBreakModeWordWrap;
headerLabel.text = header;
headerLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
UILabel * footerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
//UILabel *footerLabel = [[[UILabel alloc] initWithStyle:UITableViewCellStyleDefault] autorelease];
footerLabel.backgroundColor = [UIColor clearColor];
footerLabel.opaque = NO;
//footerLabel.textColor = [UIColor darkGrayColor];
footerLabel.textColor = [UIColor colorWithRed:0.298039 green:0.337255 blue:0.423529 alpha:1];
footerLabel.shadowColor = [UIColor whiteColor];
footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
footerLabel.font = [UIFont systemFontOfSize:14.0];
float height;
if ([UICommonUtils isiPad])
height = 40.0;
else
height = 50.0;
footerLabel.frame = CGRectMake(startLabel, 20, rectFrame.size.width - 60.0, height);
footerLabel.textAlignment = UITextAlignmentLeft;
footerLabel.lineBreakMode = UILineBreakModeWordWrap;
footerLabel.baselineAdjustment = UIBaselineAdjustmentNone;
footerLabel.numberOfLines = 0;
footerLabel.text = detail;
[customView addSubview:headerLabel];
[customView addSubview:footerLabel];
return customView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([UICommonUtils isiPad]) return 60.0;
else return 70.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return kProductHeader;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
/* ios 7 Change */
if ([cell respondsToSelector:@selector(tintColor)]) {
if (tableView == self.tableview) {
CGFloat cornerRadius = 5.f;
cell.backgroundColor = UIColor.clearColor;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (indexPath.row == 0) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
}
layer.path = pathRef;
CFRelease(pathRef);
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
lineLayer.backgroundColor = tableView.separatorColor.CGColor;
[layer addSublayer:lineLayer];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;
}
}
}
看看这个委托方法:
- (NSString *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
如果您想制作相同类型的视图(如上图所示),请在该部分的页脚中放置一个标签,使标签的背景透明并根据您的选择设置其框架并添加您想要的文本放在那里。让我给你一个可能对你有帮助的代码片段。
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = // add color of your choice
tableFooter.backgroundColor = // add color of your choice
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @" add text of your choice";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];
将上面的代码放在这个方法中:
- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section