您的位置:首页 > 产品设计 > UI/UE

iOS之UITableViewController使用详解(一)

2016-04-30 23:05 295 查看
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  =@"小魔仙";

    }

    

    cell.separatorInset=UIEdgeInsetsZero;//cell的分割线不锁进

    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 @"英雄";

}

// 可以对 section的header和 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;

}

=======系统默认tableView是显示分隔线的,但不幸的是,如果我们cell过少的话,是会出现多余的分隔线的,其实要处理这个问题,很简单,

我们只需要在tableView加载在视图后,加上下面这一句代码:

swift:

self.tableView?.tableFooterView = UIView()

OC:

self.tableView.tableFooterView = [[UIView alloc] init];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: