温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UITableView的使用

发布时间:2020-06-22 05:55:48 来源:网络 阅读:694 作者:wodenianshao 栏目:开发技术


UITableView的使用 

    今天凌晨,苹果召开2015年新品发布会。iPhone6siPhone6s Plus,以及iPad Pro、新Apple TV会与我们见面,据悉,iPhone 6siPhone6s Plus将会在918日正式发售。

 UITableView的使用

    表格视图控制器,它是一个是视图控制器的对象管理一个表视图。

一个iphone表格由三种东西构成:一个视图,一个数据源和一个委托。

数据源:它是用于管理可见的UITableview及其数据之间关系-UITableviewDataSource协议中的方法提供表格的区域,行数,顶部和底部的标题,以及每个单元格的内容等。其中有两个方法必须实现:tableView:numberOfRowsInSection:tableView:cellforRowAtIndexPath:

委托:控制表格的视觉外观和行为-UITableviewDelegate协议的对象会收到从多用户活动的通知,如选择一个单元格,编辑单元格等操作。通常我们需要实现tableViewdidSelectRowAtIndexpath

 

数据源方法:

//不重写默认返回1

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

    return 1;

}

//tableview一个区域显示多少行

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

    return [dataSourceArray count];

}

//填充数据

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

//    addMovies = [[Movie alloc]init];

//    addMovies.movieName = avcc.Addmovies.movieName;

//    addMovies.movieMoney = avcc.Addmovies.movieMoney;

//    addMovies.movieDescription = avcc.Addmovies.movieDescription;

//    [dataSourceArray addObject:addMovies];

    static NSString* Cellidentifer = @"cell";

    //cell的重用

//    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

    //系统

//    addMovies.movieName = 

    MyCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

    //自定义

    if (cell==nil) {

        //初始化一个cell

        //这里的style是一个枚举

        cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];

        //这里是在最后面加一个->

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    

    NSLog(@"%@",[dataSourceArray objectAtIndex:indexPath.row]);

//    cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];

    Movie* movie = [dataSourceArray objectAtIndex:[indexPath row]];

//    NSString* strMoney = [NSString stringWithFormat:@"%d",movie.movieMoney];

    NSLog(@"%d",[indexPath row]);

    NSLog(@"%@",[dataSourceArray objectAtIndex:[indexPath row]]);

    cell.Moviep_w_picpath.p_w_picpath = [UIImage p_w_picpathNamed:movie.movieImage];

    cell.MovieMoney.text = movie.movieMoney;

    cell.MovieName.text = movie.movieName;

    cell.MovieDescription.text = movie.movieDescription;

    

    return cell;

}

 

单元格重用机制

//填充数据

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

//    addMovies = [[Movie alloc]init];

//    addMovies.movieName = avcc.Addmovies.movieName;

//    addMovies.movieMoney = avcc.Addmovies.movieMoney;

//    addMovies.movieDescription = avcc.Addmovies.movieDescription;

//    [dataSourceArray addObject:addMovies];

    static NSString* Cellidentifer = @"cell";

    //cell的重用

//    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

    //系统

//    addMovies.movieName = 

    MyCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

    //自定义

    if (cell==nil) {

        //初始化一个cell

        //这里的style是一个枚举

        cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];

        //这里是在最后面加一个->

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    

    NSLog(@"%@",[dataSourceArray objectAtIndex:indexPath.row]);

//    cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];

    Movie* movie = [dataSourceArray objectAtIndex:[indexPath row]];

//    NSString* strMoney = [NSString stringWithFormat:@"%d",movie.movieMoney];

    NSLog(@"%d",[indexPath row]);

    NSLog(@"%@",[dataSourceArray objectAtIndex:[indexPath row]]);

    cell.Moviep_w_picpath.p_w_picpath = [UIImage p_w_picpathNamed:movie.movieImage];

    cell.MovieMoney.text = movie.movieMoney;

    cell.MovieName.text = movie.movieName;

    cell.MovieDescription.text = movie.movieDescription;

    

    return cell;

}

UITableView中显示的每一个单元都是一个UITableview对象,它的初始化函数initWithStylereuseIdentifier:比较特别,跟我们平时看到的UIView的初始化函数不同。这个主要是为了效率考虑,因为在tableview快速滑动的过程中,频繁的alloc对象是比较费时的,于是引入cell的重用机制,这个也是我们在打他source中要重点注意的地方,用好重用机制会让我们的tableView滑动起来更加流畅。

 

static NSString* Cellidentifer = @"cell";

定义一个静态字符串常量,用于指定cell的标示符.

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

从表格视图中获取带标示符的空闲(未显示)的cell,如果有则获得cell,否则cellnil

if (cell==nil) {

        //初始化一个cell

        //这里的style是一个枚举

        cell = [[MyCell alloc]initWithStyle:0 reuseIdentifier:Cellidentifer];

        //这里是在最后面加一个->

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

如果从表格视图中获取不到可用的cell,则alloc出一个新的cell,并设置标示符。

 

UITableViewCell

单元格显示类型:

typedef enum{

UITableViewStyleDefault;       à0

UITableViewCellStyleValue1;    à1

UITableViewcellStyleValue2;    à2

UITableViewCellStyleSubtitle;  à3

}



UITableView的使用

UITableView的使用

 

 

 

单元格的辅助图标类型

  //这里是在最后面加一个->

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 

UITableView的使用

UITableView的使用


 

 

编辑单元格(增,删,移动)

1. 设置tableview的编辑模式:

 

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用


 

 

 

 

 

选中单元格—>进入二级视图进行修改信息

 

UITableView的使用

UITableView的使用

 

 

更新单元格内容

UITableView的使用

UITableView的使用

 

 

 

 

 

 

需要先引入协议

 

UITableView的使用

UITableView的使用

 

 

 

分段按钮实现

 

 

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用


 

 

 

 

 

 

自定义单元格

 

 

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用

UITableView的使用


 

 

UITableView的使用

UITableView的使用


 

 

 

 

 

 

 

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI