UITableViewController使用详解

来源:互联网 发布:oracle数据库添加字段 编辑:程序博客网 时间:2024/06/15 20:11

UITableView的多组显示


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    

//tintColor 很多控件都有

    

    // 设置tableView的索引标题颜色

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

  

    // 设置tableView的索引标题背景颜色 

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

   

    // 设置tableView的索引标题选中时的颜色  

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

    

    // 设置主题颜色 tintColor

    self.tableView.tintColor =HMColor(21,188, 173);

    

   }



// 有多少组在tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    

    return 3;

}



// 每一组有多少行

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

    /**

     因为每一组显示的行数不一样,所以要对每一组进行判断

     */

    NSInteger rows = 0;

    

    if (section == 0) {

        rows = 30;

    } else if (section ==1) {

        rows = 20;

    } else {

        rows = 10;

    }


    return rows;

}


// 每一行显示的内容

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

    // 实例化cell

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];

    

   /**

    indexPath.row   

    indexPath.section 

    定义唯一的一行

    

    */

    // 把组取出来

    NSInteger sectionIndex = indexPath.section;

    

    if (sectionIndex == 0) { // 0

        // 判断第0组的第几行

        NSInteger rowIndex = indexPath.row;

        

        if (rowIndex == 0) {

            cell.textLabel.text =@"火影:鸣人";

        } else if (rowIndex ==1) {

            cell.textLabel.text =@"上忍:卡卡西";

        } else {

            cell.textLabel.text =@"中忍:";

        }

    } else if (sectionIndex ==1) {

        // 两行

        

        if (indexPath.row ==0) {

            cell.textLabel.text =@"风影:我爱罗";

        } else {

            cell.textLabel.text =@"堪九郎";

        }

    } else {

        cell.textLabel.text  =@"小魔仙";

    }

    

    return cell;

}


/**

 titleForHeaderInSection: 组的头部显示的文本

 */

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    

    // 判断是第几组,然后显示对应的文本

    NSString *title = @"";

    

    switch (section) {

        case 0:

        {

            title = @"火之国";

        }

            break;

        case 1:

        {

            title = @"风之国";

        }

            break;

        case 2:

        {

            title = @"巴拉巴拉";

        }

            break;

            

        default:

            break;

    }

    

    

    

    return title;

    

}


/**

 titleForFooterInSection: 组的头部显示的文本

 */

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    // 判断是第几组,然后显示对应的文本

    NSString *title = @"";

    

    switch (section) {

        case 0:

        {

            title = @"五影最强";

        }

            break;

        case 1:

        {

            title = @"黑眼圈";

        }

            break;

        case 2:

        {

            title = @"巴拉巴拉小魔仙";

        }

            break;

            

        default:

            break;

    }

    

    

    

    return title;

}


- (BOOL)prefersStatusBarHidden {

    return YES;

}


@end


// 设置控制器成为tableView的代理

    _tableView.delegate =self;

    

    // 设置分割线颜色的

    _tableView.separatorColor = [UIColorredColor];

    

    // 侵蚀 , 分割线样式

    _tableView.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

    

    // top, left, bottom, right , 上, 是没有效果的

    _tableView.separatorInset =UIEdgeInsetsMake(0,0,0, 0);

    

    // 允许多选

    _tableView.allowsMultipleSelection =YES;

    

    /**

     如果让tableiew自动的计算行高,那么就必须给他一个预估的行高

     

     如果设置了预估行高,那么tableview在去加载数据的时候, 不会频繁的调用 heightForRowAtIndexPath:

     

     先调用一次 cellForRowAtIndexPath:

     再调用一次 heightForRowAtIndexPath:

     */

    self.tableView.estimatedRowHeight =10;

    

    // UITableView 自动的去计算cell的高度

    self.tableView.rowHeight =UITableViewAutomaticDimension

    // 设置行高 ,(静态设置)如果每个cell的高度都一样,推荐这种设置

//    _tableView.rowHeight = 100;

    

    //设置tableView的头

    UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100, 100)];

    [headerView setBackgroundColor:[UIColororangeColor]];

    

    _tableView.tableHeaderView = headerView;

    

    //设置tableView的尾


    UIView *footerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100, 100)];

    [footerView setBackgroundColor:[UIColoryellowColor]];

    

    _tableView.tableFooterView = footerView;

//设置cell的样式(系统自带的)

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

    /**

     UITableViewCellStyleDefault : 不显示detailTextLabel

     UITableViewCellStyleValue1 detailLabel 显示在 textLabel 右侧

     UITableViewCellStyleValue2 imageView不再显示, textLabel居左变蓝色

     UITableViewCellStyleSubtitle :都显示, detailLabel textLabel下侧

     */


// 设置cell上控件的内容

    HeroModel *model = self.dataArray[indexPath.row];

    

    // 设置imageView

    cell.imageView.image = [UIImageimageNamed:model.icon];

    

    // 设置文本

    cell.textLabel.text = model.name;

    

    // 设置detailTextLabel

    cell.detailTextLabel.text = model.intro;

    

    // 设置右侧箭头

    // accessory : 配件

    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    

    // 设置选择样式

    /**

     UITableViewCellSelectionStyleNone,

     UITableViewCellSelectionStyleBlue,  用灰色来代替了

     UITableViewCellSelectionStyleGray,

     UITableViewCellSelectionStyleDefault

     cell.selectionStyle = UITableViewCellSelectionStyleBlue;

     */

    

    /**

     设置选中的背景view

     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];

     tempView.backgroundColor = [UIColor yellowColor];

     

     cell.selectedBackgroundView = tempView;

     */

    

    // cell.backgroundColor = [UIColor yellowColor];

    

    /**

     accessoryView 自定义控件

     自定义 accessoryView的时候, frame中的坐标(x,y)修改后无效

     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

     tempView.backgroundColor = [UIColor redColor];

     

     cell.accessoryView = tempView;

     */

    

组的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    

    return @"英雄";

}


// 可以对 sectionheader footer设置view

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

    

    

    UIView *headerView = [[UIViewalloc]init];

    [headerView setBackgroundColor:[UIColorredColor]];

    

    return headerView;

}


组标题快速索引

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    

    return_indexArray;

    

}


// 滚动到最后一行

        [_tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];


#pragma mark -  返回cell行高的代理方法

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

    // 要返回所有cell的行高

    // 取出contentFrameModel

    ContentFrameModel *frameModel = self.dataArray[indexPath.row];

    

    

    return frameModel.cellHeight;

}



1 0
原创粉丝点击