UITableView的使用-创新互联

UITableView的使用

创新互联从2013年创立,公司以做网站、网站设计、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户千余家,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。

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

 UITableView的使用

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

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

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

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

数据源方法:

//不重写默认返回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对象,它的初始化函数initWithStyle:reuseIdentifier:比较特别,跟我们平时看到的UIView的初始化函数不同。这个主要是为了效率考虑,因为在tableview快速滑动的过程中,频繁的alloc对象是比较费时的,于是引入cell的重用机制,这个也是我们在打他source中要重点注意的地方,用好重用机制会让我们的tableView滑动起来更加流畅。

static NSString* Cellidentifer = @"cell";

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

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifer];

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

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的使用

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:UITableView的使用-创新互联
URL网址:http://lszwz.com/article/gcpjo.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款