UITableView 应用及其总结

Plain:

Grouped:

Cell的结构图:

UITableViewCellStyleDefault:预设使用这种,若左侧ImageView没图的话,只有一行字(textLable.text)。

UITableViewCellStyleValue1:左侧为textLable.text并且左对齐,右侧为detailTextLable.text并且右对齐。

UITableViewCellStyleValue2:左侧为detailTextLable.text,右侧为textLable.text并且左对齐。

UITableViewCellStyleSubtitle:跟UITableViewCellStyleDefault大致相同,detailTextLable.text出现在textLable.text下方。

如何使用:

    - (void)dealloc
    {
         [itemsArray release];
         [super dealloc];
    }  

    - (void)viewDidLoad
    {
       [super viewDidLoad];  

       //初始化资料阵列,待会使用
       NSMutableArray *itemsArray = [[NSArray alloc] initWithObjects:@"row 1",@"row 2",@"row 3",nil];  

    }  

    #pragma mark - Table view data source  

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

        // Return the number of sections.
        // 告诉tableView总共有多少个section需要显示
        return 1;
    }  

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

        // Return the number of ro??ws in the section.
        // 告诉tableView一个section里要显示多少行
        return [itemsArray count];
    }  

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //cell的标饰符
        static NSString *CellIdentifier = @"cellIdentifier";  

        //指定tableView可以重用cell,增加性能,不用每次都alloc新的cell object
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

        //如果cell不存在,从预设的UITableViewCell Class里alloc一个Cell object,应用Default样式,你可以修改为其他样式
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }  

        // Configure the cell...  

        //每一行row进来都判定一下,分别依次选用不同的图片
        switch (indexPath.row) {
            case 0:
            {
                cell.imageView.image = [UIImage imageNamed:@"image0.png"];
            }
                break;
            case 1:
            {
                cell.imageView.image = [UIImage imageNamed:@"image1.png"];
            }
                break;
            case 2:
            {
                cell.imageView.image = [UIImage imageNamed:@"image2.png"];
            }
                break;
            default:
            {
                cell.imageView.image = [UIImage imageNamed:@"default.png"];
            }
                break;
        }  

        //其他相同的属性一并设定
        cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];  

        //设字体、颜色、背景色什么的
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor colorWithRed:54.0/255.0 green:161.0/255.0 blue:219.0/255.0 alpha:1];
        cell.detailTextLabel.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1];  

        //设定textLabel的最大允许行数,超过的话会在尾未以...表示
        cell.textLabel.numberOfLines = 2;  

        return cell;
    }  

    //这个是非必要的,如果你想修改每一行Cell的高度,特别是有多行时会超出原有Cell的高度!
    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 85.0;
    }  

如果比较进阶一点的,想修改或者增加更多的component进去Cell里面,有两种方法(大同小异):
第一种是在cellForRowAtIndexPath delegate method里alloc cell时在contentView里面addSubView。

第二种是需要继承Cell作一个Custom Cell 的Class,并可以使用layoutSubview等方法来修改component的frame呢。

http://blog.csdn.net/titer1991/article/details/7945127

UITableView 应用及其总结

时间: 2024-08-17 10:41:17

UITableView 应用及其总结的相关文章

iOS开发——项目实战总结&UITableView性能优化与卡顿问题

UITableView性能优化与卡顿问题 1.最常用的就是cell的重用, 注册重用标识符 如果不重用cell时,每当一个cell显示到屏幕上时,就会重新创建一个新的cell 如果有很多数据的时候,就会堆积很多cell.如果重用cell,为cell创建一个ID 每当需要显示cell 的时候,都会先去缓冲池中寻找可循环利用的cell,如果没有再重新创建cell 2.避免cell的重新布局 cell的布局填充等操作 比较耗时,一般创建时就布局好 如可以将cell单独放到一个自定义类,初始化时就布局好

iOS开发tips-神奇的UITableView

概述 UITableView是iOS开发中使用频率最高的UI控件,在前面的文章中对于UITableView的具体用法有详细的描述,今天主要看一些UITableView开发中的常见一些坑,这些坑或许不深,但是如果开发中注意不到的话往往比较浪费时间. 神奇的section header 事情的起因是一个网友说要实现一个类似下图界面,但是不管是设置sectionHeaderHeight还是代理方法中实现func tableView(_ tableView: UITableView, heightFor

ios UISearchDisplayController 实现 UITableView 搜索功能

UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都 封装好了,使用起来非常非常的简单.特别是要实现全屏搜索时使用最多. 全屏搜索的意思是如果你用了  NavigationBar 当点击搜索框时 TableView 会自动弹上去

iOS开发——仿Clear纯手势操作的UITableView

前言 在Clear应用中,用户无需任何按钮,纯靠不同的手势就可以完成对ToDoItem的删除.完成.添加.移动.具体来说,功能上有左划删除,右划完成,点击编辑,下拉添加.捏合添加.长按移动.这里将这些功能实现并记录. 左划删除与右划完成 所谓的左右滑动,就是自定义一个cell然后在上面添加滑动手势.在处理方法中计算偏移量,如果滑动距离超过cell宽度一半,就删除它,或者是为文本添加删除线等来完成它:如果没有超过一半,那么就用动画把cell归位. 效果图如下: 关键代码如下: - (void)ha

iOS UITableView表视图(1)

//在.h文件中声明一下 //例如:@property(nonatomic,strong)UITableView *table; //创建一个UITableView self.table = [[UITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)]; //设置行的高度 self.table.rowHeight = 260.0; //设置分割线的颜色 self.table.separatorColor

UITableView

1.如何设置tableview  每行之间的分割线 self.table.separatorStyle=UITableViewCellSeparatorStyleSingleLine; 2.如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置 法一:完全不变色 cell.selectionStyle  =  UITableViewCellSelectionStyleNone: 法二:变下色马上恢复 [tableView deselectRowAtIndexPath:

iOS开发UI篇—UITableview控件基本使用

一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h> 2 3 @interface NJHero : NSObject 4 /** 5 * 头像 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称 10 */ 11 @property (nonatomic, copy) NSString *name; 12 /** 13 * 描述 1

iOS开发UI篇—UITableview控件简单介绍

一.基本介绍 在众多移动应?用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,?且性能极佳 . UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置. 二.数据展示 UITableView需要?一个数据源(dataSource)来显示数据UITableView会向数据源查询一共有多少行数据以及每?行显示什么数据等 没有设置数据源

iOS 中UITableView的深理解

例如下图:首先分析一下需求:1.根据模型的不同状态显示不同高度的cell,和cell的UI界面. 2.点击cell的取消按钮时,对应的cell首先要把取消按钮隐藏掉,然后改变cell的高度. 根据需求先解决第一个需求,需要两步 当模型数据的属性的status [email protected]"2",不显示取消按钮:status = @"1",显示取消按钮. 1.需要注意的是cell的重用在这里面互有一些影响,所以在自定义cell的模型的setter方法中, 在ce

iOS开发UI篇—实现UItableview控件数据刷新

iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运行界面: 点击选中行: 修改数据后自动刷新: 三.代码示例 数据模型部分: YYheros.h文件 // // YYheros.h // 10-英雄展示(数据刷新) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. A