UI基础之UITableViewController

UITableViewController在实际项目中很重要

tableView上面不能加子视图addSubviews 所有的都要在cell上加来实现

程序并不知道有多少数据要放入,所以要通过一个代理来实现数据的传送

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 数据数组*/

@property(nonatomic,strong) NSArray * dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];  

    //加载数据

    [self loadData];

    //加载子视图

    [self setSubviews];

}

#pragma mark - 加载数据

- (void)loadData{

    self.dataArray= [UIFont familyNames];

}

#pragma mark - 加载子视图

- (void)setSubviews{

     UITableView * tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];

    //设置属性代理

    tableView.delegate=self; 

    //设置数据源代理

    tableView.dataSource=self;  

    [self.view addSubview:tableView];

}

#pragma mark - 实现tabView代理方法

#pragma mark  * 返回需要展示的数据行数

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

    return self.dataArray.count;

}

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

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

    

static NSString * identy=@"jrcell";

    //上面tableView已经传入了

    //先根据当前的标志去缓冲池去寻找对应的cell

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identy];

    

    if (cell==nil) {

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

        

        cell.backgroundColor=[UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0  blue:arc4random()%255/255.0  alpha:1];

        NSLog(@"=====%ld",indexPath.row);

    }

    //根据当前的cell的行数,取出对应的数组数据

    NSString * text=self.dataArray[indexPath.row];

    

    //将数据赋值给cell

    cell.textLabel.text=text;

    

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

    

    return cell;

    

 

}

 

@end

 

系统给出的cell无法满足设计要求时 要进行cell自定义

 

 

以及tableView的各种代理方法的实现

 

#pragma mark - tableView 代理方法

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

    

    //唯一标示

    static NSString * identy=@"cell";

 

    JRTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];

    if (cell==nil) {

        

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

    }

    

    cell.news=self.newsArray[indexPath.row];

    cell.textLabel.textColor=[UIColor redColor];

    

//    //判断当前cell是不是在选中队列当中

    if ([self.selectArray containsObject:@(indexPath.row)]) {

        cell.accessoryType=UITableViewCellAccessoryCheckmark;

    }else{

        cell.accessoryType=UITableViewCellAccessoryNone;

    }

 

    return cell;

    

}

 

 

 

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

 

    return self.newsArray.count;

    

}

 

 

 

 

#pragma mark - 导航按钮点击事件

#pragma mark * 左边

- (void) leftClickAction{

    

    //给数组增加数据

    JRNews * news=[[JRNews alloc] init];

    news.time=@"4小时前";

    news.title=@"aahaaha";

    news.imgPath=@"http://cdn.duitang.com/uploads/item/201510/11/20151011195344_aVZRx.jpeg";

    [self.newsArray insertObject:news atIndex:0];

    

    

    NSIndexPath * indexPath=[NSIndexPath indexPathForRow:0 inSection:0];

    

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

    

 

}

 

#pragma mark * 右边

- (void) rightClickAction{

    [self.tableView setEditing:YES animated:YES];

}

 

 

 

 

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

    

    //1 根据当前的indexPath 获取对应的cell

     JRTableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];

    //2 cell添加选中附件

     cell.accessoryType=UITableViewCellAccessoryCheckmark;

    

    //3 将选中的当前索引放入数组

    [self.selectArray addObject:@(indexPath.row)];

    

}

 

 

#pragma mark - cell编辑

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if (editingStyle==UITableViewCellEditingStyleDelete) {

      

        

        [self.newsArray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

        

        

        

    }else if(editingStyle==UITableViewCellEditingStyleInsert){

        NSLog(@"你点击了增加");

    }else{

        NSLog(@"我不知道你点击了什么");

    }

    

}

 

#pragma mark - 返回编辑类型

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    return UITableViewCellEditingStyleDelete;

 

}

 

 

#pragma mark - 设置删除按钮文本

- (NSString *) tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    if(indexPath.row%2){

        return @"奇数";

    }else{

        return @"偶数";

 

    }

}

 

//泛型

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

 

    UITableViewRowAction * action=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"点击置顶");

    }];

    action.backgroundColor=[UIColor grayColor];

    

    

    UITableViewRowAction * action2=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"发送" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"点击发送");

    }];

    

     action2.backgroundColor=[UIColor greenColor];

    

    

    return @[action,action2];

 

}

原文地址:https://www.cnblogs.com/gzoof/p/5586844.html