如何将框架更改为标题标题(UITableView)

时间:2016-11-04 14:31:45

标签: ios swift uitableview

如何在swift中将x轴更改为标题标题部分?

我无法使用该代码更改它:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.textLabel!.textColor = UIColor.darkGrayColor()
    header.textLabel!.font = UIFont(name: "Arial", size: 12.0)!
    header.textLabel!.textAlignment = NSTextAlignment.Left

    header.backgroundView?.backgroundColor = UIColor.sectionGray()

    //Not working
    header.textLabel!.frame.origin.x = header.textLabel!.frame.origin.x - 60

}

3 个答案:

答案 0 :(得分:1)

header.textLabel!.frame = header.textLabel!.frame.offsetBy(dx: -60, dy: 0)

答案 1 :(得分:0)

//Not working
    header.textLabel!.frame.origin.x = header.textLabel!.frame.origin.x - 60

您无法更改orign.x或orign.y,但您可以更改框架或更新框架

做一件事,而不是这个opproach

在你的故事板中

创建一个uitableviewCell并根据需要进行设计,并将其类和idntifire定义为SectionHeader

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


    SectionHeaderTableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:@"SectionHeader"];

    if (headerView == nil) {
        headerView = (SectionHeaderTableViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SectionHeader"];
    }

    headerView.headerTxt.text=@"THIS WEEK";
        return headerView;
}

这是客观的C代码转换为swift这是最简单的方式来玩标题

答案 2 :(得分:0)

使用exith中的视图,例如

enter image description here

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *tableView;
@property (strong, nonatomic) IBOutlet UITableView *myTable;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *tblCell = [tableView dequeueReusableCellWithIdentifier:@"gm" forIndexPath:indexPath];
    return tblCell;
}
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    return self.tableView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

enter image description here

相关问题