UITabeViews---設置字體格式,大小,顏色

效果圖:


UITableView設置每行顯示的內容,字體格式,大小,顏色

首先設置根視圖控制器:

AppDelegate.m文件


#import "AppDelegate.h"

#import "JRTableViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    

    JRTableViewController * tableVC=[[JRTableViewController alloc]init];

    self.window.rootViewController=tableVC; 

    return YES;

}




自定義的JRTableViewController.m文件


#import "JRTableViewController.h"


//定義宏

#define jrRandomColor [UIColor colorWithRed:arc4random_uniform(10)*0.1 green:arc4random_uniform(10)*0.1  blue:arc4random_uniform(10)*0.1  alpha:1]


@interface JRTableViewController ()


//數據存儲

@property (nonatomic,strong) NSArray * dataArray;


@end


@implementation JRTableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.tableView.rowHeight=100;

    

    //加載數據

    [self _loadData];

    

    

}


#pragma mark - 加載 tableView 數據

- (void) _loadData

{

    self.dataArray=[UIFont familyNames];  //每行cell內顯示的內容

}



//創建JRTableViewController時,自動生成代理方法

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataArray.count//返回數組的行數

}




#pragma mark - 返回cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString * identy=@"JRTable";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];

    if (!cell)

    {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];

    }

    cell.textLabel.text=self.dataArray[indexPath.row];

    cell.textLabel.font=[UIFont fontWithName:cell.textLabel.text size:16];

    

    

    //設置字體顏色

    if(indexPath.row%2==0)

    {

        cell.textLabel.textColor=jrRandomColor//

    }

    

    return cell;

}


//設置每一行的高度

/*

  0  高度 100

  1  高度 50

  2  高度 100

  3  高度 50

  4  高度 100

  5  高度 50

 */

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row%2==0)

    {

        return 100;

    }

    else

    {

        return 50;

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}



@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章