如何在UITableView的后台视图(主要说明)中编写

时间:2013-10-15 12:35:40

标签: iphone ios objective-c uitableview

我想实现类似的目标

enter image description here

主要用于指令目的,是否有我不知道的UITableView方法,或者有某种技巧。

4 个答案:

答案 0 :(得分:2)

这是在UItableView的页眉和页脚中编写的。因此,请自定义表格中各节的headerView和footerView。

使用以下方法:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

希望它对你有所帮助。

答案 1 :(得分:0)

查看此委托方法:

- (NSString *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

答案 2 :(得分:0)

如果你想制作同样的视图(如上图所示),在标题的页脚中放一个标签,使标签的背景透明,并根据你的选择设置框架,并添加文字你想放在那里。让我给你一个可能对你有帮助的代码片段。

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

答案 3 :(得分:0)

您可以在我的应用中使用以下方法:

- (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;
        }
    }
}