uicllectionViewiOS开发程序员

Day.03.03 UITableView 表视图属性

2016-03-03  本文已影响292人  挂树上的骷髅怪

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1⃣️.创建表视图
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //2⃣️.设置代理对象
    
    tableView.dataSource = self;//数据源协议:显示内容
    
    tableView.delegate = self;//代理协议:表视图功能
    
    //3⃣️属性
    
        //1.UIView的所有属性
    
        //2.分割线颜色
    tableView.separatorColor = [UIColor blackColor];
    
        /**
         * 3.分割线样式
         
     UITableViewCellSeparatorStyleNone,               无分割线
     UITableViewCellSeparatorStyleSingleLine,           单线(默认)
     UITableViewCellSeparatorStyleSingleLineEtched      Grouped下辅助线
         */
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    
        //4.设置所有行高 默认44  (宽度是表视图的宽)
    tableView.rowHeight = 100;
    
        //5.设置头/尾视图 (frame 只有高度有效)
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
    
    headerView.backgroundColor = [UIColor yellowColor];
    
    tableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
    
    footerView.backgroundColor = [UIColor yellowColor];
    
    tableView.tableFooterView = footerView;
    
        //6.组头/尾视图 高度
    tableView.sectionHeaderHeight = 20;
    
    tableView.sectionFooterHeight = 20;
    
    
    //4⃣️.添加并显示
    [self.view addSubview:tableView];
    
}

#pragma mark-- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //1.创建
    
    /**
        单元格样式
     
     UITableViewCellStyleDefault,   //text label - image view
     
     UITableViewCellStyleValue1,        // 图 - text - detailText
     
     UITableViewCellStyleValue2,        // text (偏右) - detailText (偏左)
     
     UITableViewCellStyleSubtitle   // 图 - text
     detailText
     */
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    
    //2.自定义颜色
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    
        //图片
    cell.imageView.image = [UIImage imageNamed:@"qqmusic"];
    
        //主标题
            //IndexPath 表示索引路径 一个IndexPath可以代表一个Cell的位置
    cell.textLabel.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
    
        //副标题
    cell.detailTextLabel.text = @"detail";
    
        //选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        //选中的背景视图
    //    cell.selectedBackgroundView =
    
        //辅助视图
    /**
     *  UITableViewCellAccessoryNone,                 无
     UITableViewCellAccessoryDisclosureIndicator 右向箭头
     UITableViewCellAccessoryDetailDisclosureButton 信息按钮+右箭头
     UITableViewCellAccessoryCheckmark             对号
     UITableViewCellAccessoryDetailButton        信息按钮
     */
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //3.返回
    return cell;
    
}

@end
屏幕快照 2016-03-03 上午9.26.09.png
上一篇 下一篇

猜你喜欢

热点阅读