tableView

- (NSArray *)wineArray

{

    if (!_wineArray) {

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wine.plist" ofType:nil]];

        

        NSMutableArray *temp = [NSMutableArray array];

        for (NSDictionary *wineDict in dictArray) {

            [temp addObject:[LZJWine wineWithDict:wineDict]];

        }

        

        _wineArray = temp;

    }

    return _wineArray;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // 设置tableView每一行cell的高度

    self.tableView.rowHeight = 100;

    

    // 设置tableView每一组的头部高度

    self.tableView.sectionHeaderHeight = 80;

    // 设置tableView每一组的尾部高度

//    self.tableView.sectionFooterHeight = 80;

    

    // 设置分割线的颜色

//    self.tableView.separatorColor = [UIColor redColor];

    

    // 设置分割线的样式

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    

    // 设置表头控件

    self.tableView.tableHeaderView = [[UISwitch alloc] init];

    // 设置表尾控件

    self.tableView.tableFooterView = [[UISwitch alloc] init];

}

 

#pragma mark - UITableViewDataSource

 

// section == 0

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

{

    return self.wineArray.count;

}

 

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

{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    

    // 设置右边显示的指示样式

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // 设置右边显示的指示控件

    cell.accessoryView = [[UISwitch alloc] init];

    // 设置选中样式

//    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    

    // 设置cell的背景View

    // backgroundView的优先级 > backgroundColor

//    UIView *bg = [[UIView alloc] init];

//    bg.backgroundColor = [UIColor blueColor];

//    cell.backgroundView = bg;

    

    // 设置cell的背景颜色

//    cell.backgroundColor = [UIColor redColor];

    

    // 设置cell选中时候的背景view

//    UIView *selectedBg = [[UIView alloc] init];

//    selectedBg.backgroundColor = [UIColor greenColor];

//    cell.selectedBackgroundView =  selectedBg;

    

  

 

    // 取出indexPath 对应的酒模型

    LZJWine *wine = self.wineArray[indexPath.row];

    

    // 设置数据

    cell.textLabel.text = wine.name;

    cell.imageView.image = [UIImage imageNamed:wine.image];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money];

    cell.detailTextLabel.textColor = [UIColor orangeColor];

    

    return cell;

}

 

#pragma mark - UITableViewDelegate

/**

 *  选中了某一行cell就会调用这个方法

 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"选中了:%ld",indexPath.row);

}

 

/**

 *  取消选中了某一行cell就会调用这个方法

 */

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

{

//     NSLog(@"取消选中了:%ld",indexPath.row);

}

 

/**

 *  返回每一组显示的头部控件

 */

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

{

    return [UIButton buttonWithType:UIButtonTypeContactAdd];

}

 

/**

 *  返回每一组显示的尾部控件

 */

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

//{

//    

//}

 

/**

 *  返回每一组的头部高度

 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 100;

}

 

/**

 *  返回每一组的尾部高度

 */

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

//{

//    

//}

 

/**

 *  返回每一行cell的高度

 */

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

//{

//    if (indexPath.row % 2 == 0) {

//        return 100;

//    } else {

//        return 50;

//    }

//}

 

 

—————————

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UITableView *tableView = [[UITableView alloc] init];

    tableView.frame = CGRectMake(50, 100, 200, 300);

    tableView.backgroundColor = [UIColor grayColor];

    tableView.dataSource = self;

    tableView.delegate = self;

    [self.view addSubview:tableView];

    

    // 内边距

//    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);

    

    // header - footer

    UIView *header = [[UIView alloc] init];

    header.frame = CGRectMake(0, 0, tableView.frame.size.width, 64);

    header.backgroundColor = [UIColor yellowColor];

    tableView.tableHeaderView = header;

    

//    UIView *footer = [[UIView alloc] init];

//    footer.frame = CGRectMake(0, 0, tableView.frame.size.width, 49);

//    footer.backgroundColor = [UIColor yellowColor];

//    tableView.tableFooterView = footer;

    

    // 额外添加的子控件

    UIView *header2 = [[UIView alloc] init];

    header2.frame = CGRectMake(0, -64, tableView.frame.size.width, 64);

    header2.backgroundColor = [UIColor blueColor];

    [tableView addSubview:header2];

}

 

#pragma mark - <UITableViewDelegate>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    NSLog(@"contentOffset.y = %f", scrollView.contentOffset.y);

}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"contentSize.height = %f", tableView.contentSize.height);

}

posted @ 2016-04-29 15:22  suifenglangzi  阅读(148)  评论(0编辑  收藏  举报