UITableView UITableViewCell

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

自定义UITableViewcell的方法

1、创建cell时,不从重用池找,进来就创建

NSString *identifier = [NSString stringWithFormat:@"cell"]; // 设置cell 标识

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];  // 创建cell的同时为其附上设置的标识,进来就创建带有相同标识的cell

这种方法很耗内存,为了解决它。引用重用池,我们把之前创建好的cell放进重用池内,下面在用,直接从重用池中拿出来。拿出来后,此cell便不在重用池内。也就是说内容为“123”的cell被拿出来后,下一个显示的cell是上面又一次滑进去的空的cell。然而并不是上面每滑进去一个,下面就能拿出来用。而是当下面要‘ 创建’的这个cell 的标示符 与 上面被滑进去的cell中的某一cell的标识符一致时(按标识找),重用池中的这个cell才会被拉出去用。否则就生产一个带这个标识的cell。

2. 为了解决被找到拿出来用的cell有原内容

a、拿出来用之前,把原内容清空  cell.textLabel.text = nil;

b、给创建的每一个cell赋不同的标识

NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识。进入此方法,下一个要显示的cell就被赋了一个唯一标识,比如cell0组15行,与上面滑进去cell的标识都不同。它会被重新创建

c.、进入重用池找的时候,不用标示符

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//处理重用bug很不友好的方式,不建议使用

王洋洋  16:56:50

// 用自定义cell 可以充分任意利用一个cell 长条,为其添加控件,并为控件分配空间。如果不使用自定义cell,那么可以对cell操作的只有添加图片 cell.imageView.image  、显示标题 cell.TextLabel.text 、显示详细内容 cell.detailText.text、(并只有四种显示Style: Default subtitle  value1  value2)等小东东,

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

1.自定义一个继承uitableviewcell的类

2.在.h文件中声明cell控件属性 //自定义cell,在.h里进行控件属性的声明

@property (nonatomic, strong) UIImageView *newsImg;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subLabel;

3. //自定义cell,在.m里进行控件的初始化

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
   
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _newsImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 100, 80)];
        [self addSubview:_newsImg];
       
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(115, 5, 200, 30)];
        [self addSubview:_titleLabel];
       
        _subLabel = [[UILabel alloc] initWithFrame:CGRectMake(115, 55, 200, 30)];
        [self addSubview:_subLabel];
    }
    return self;

}

4.在所用的文件中把uitableviewcell改成自定义的类,以及所用的控件类型都要改成自定义中的控件

#import "ViewController.h"
#import "NewsInfo.h"
#import "MyTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
   
    NSArray *_array;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

UITableView *_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
   
    [self.view addSubview:_tableView];
   
   
    NewsInfo *info = [[NewsInfo alloc] init];
    info.imageName = @"1.jpg";
    info.title = @"马寨拆迁";
    info.subTitle = @"马寨拆迁,热火朝天";
   
    NewsInfo *info1 = [[NewsInfo alloc] init];
    info1.imageName = @"2.jpg";
    info1.title = @"大马寨拆迁";
    info1.subTitle = @"马寨拆迁,热火朝天";

_array = @[info,info1];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    static NSString *identifier = @"Cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
   
    NewsInfo *_info = _array[indexPath.row];
   
    cell.newsImg.image = [UIImage imageNamed:_info.imageName];
   
    cell.titleLabel.text = _info.title;
    cell.subLabel.text = _info.subTitle;
   
    return cell;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;

}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

如果在文件中需要添加的图片过多这是需要创建一个数组(数组中包含标题,图片,以及副标题。

这时可以1.先建一个无关的类 以及属性

@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subTitle;

这是在使用数组时只需将其中的变量取出用一下方法(下标)

NewsInfo *_info = _array[indexPath.row];
   
    cell.newsImg.image = [UIImage imageNamed:_info.imageName];
   
    cell.titleLabel.text = _info.title;

cell.subLabel.text = _info.subTitle;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//1。创建  两种格式UITableViewStylePlain和UITableViewStyleGrouped,其本质上没多大区别,主要是界面风格

UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //2。行高
    table.rowHeight  = 100;
   
    //3设置分割线的样式风格//很多种自己选择
    table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //4设置分割线的颜色
    table.separatorColor = [UIColor blackColor];
    //5给table添加背景 可以不设置背景的frame
      //5.1第一种: UIView *view =[[UIView alloc]init];
            //view.backgroundColor = [UIColor grayColor];
    //5.2第二种
    UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    table.backgroundView = view;
    //6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    view2.backgroundColor = [UIColor clearColor];
    table.tableHeaderView = view2;
    //7设置代理
    table.delegate =self;
    //8设置数据源
    table.dataSource = self;
   
    table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
   
    [self.view addSubview:table];
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else if (section ==1){
        return 5;
    }else if(section ==2){
        return 6;
    }
   
    return  4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
    static NSString *identifier = @"cell";//重用机制标识
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
                //cell的四种样式
                //UITableViewCellStyleDefault,只显示图片和标题
                //UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
                //UITableViewCellStyleValue2,标题和子标题
               
                //UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
            reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
    }
    cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
    cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
   
      //cell.imageView.image = [UIImage imageNamed:@"012"
                              //];//给每组每行添加图片
   
    cell.detailTextLabel.text [email protected] "who are you";//设置子标题
    if (indexPath.section==0 &&indexPath.row==0) {
        cell.imageView.image = [UIImage imageNamed:@"21"];
    }
    if (indexPath.section ==1 && indexPath.row ==2) {
        cell.imageView.image =[UIImage imageNamed:@"2"];
    }//向第几组,第几行加图片。
   
   
  //1  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
   
   //2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
   //3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
   
  //4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
   //5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
   
        return cell;//将设置好的cell返回
   
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
   
    return view;//自定义每组头视图
   
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}

//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        return 100;
    }
    return 30;
   
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSLog(@"%zi",section);
    return [NSString stringWithFormat:@"%zi",section];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSMutableArray    *_array;//盛放要显示的东西
   
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor grayColor];
    button.frame = CGRectMake(300, 20, 60, 60);
    [button setTitle:@"+" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
    NSLog(@"=====");
    [_array addObject:@"123"];
    [_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
   // cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
   
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor blueColor];
    cell.selectedBackgroundView = view;//自定义cell选中背景
   
    cell.textLabel.text = _array[indexPath.row];

cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
   
   // cell.accessoryView  自定义右侧视图
   
    return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
   
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"----%zi",indexPath.row);
}

//cell被选中 //  点击cell时触发一个方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
NSLog(@"----%zi",indexPath.row);
   
    [tableView
deselectRowAtIndexPath:indexPath animated:YES];//cell被点击时,取消选中状态

}

//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//提交修改动作:  再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [_array removeObjectAtIndex:indexPath.row];
   
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell(有bug 解决bug用一下 1 2 3)

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.dataSource = self;
    [self.view addSubview:table];
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   //1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识

static NSString *identifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    cell.textLabel.text = nil;//3 先清理,在使用
   
    if (indexPath.row == 1) {
        cell.textLabel.text = @"1212";
    }
   
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//1。创建  两种格式UITableViewStylePlain和UITableViewStyleGrouped,其本质上没多大区别,主要是界面风格
    UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //2。行高
    table.rowHeight  = 100;
   
    //3设置分割线的样式风格//很多种自己选择
    table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //4设置分割线的颜色
    table.separatorColor = [UIColor blackColor];
    //5给table添加背景 可以不设置背景的frame
      //5.1第一种: UIView *view =[[UIView alloc]init];
            //view.backgroundColor = [UIColor grayColor];
    //5.2第二种
    UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    table.backgroundView = view;
    //6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    view2.backgroundColor = [UIColor clearColor];
    table.tableHeaderView = view2;
    //7设置代理
    table.delegate =self;
    //8设置数据源
    table.dataSource = self;
   
    table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
   
    [self.view addSubview:table];
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else if (section ==1){
        return 5;
    }else if(section ==2){
        return 6;
    }
   
    return  4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
    static NSString *identifier = @"cell";//重用机制标识
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
                //cell的四种样式
                //UITableViewCellStyleDefault,只显示图片和标题
                //UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
                //UITableViewCellStyleValue2,标题和子标题
               
                //UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
            reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
    }
    cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
    cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
   
      //cell.imageView.image = [UIImage imageNamed:@"012"
                              //];//给每组每行添加图片
   
    cell.detailTextLabel.text [email protected] "who are you";//设置子标题
    if (indexPath.section==0 &&indexPath.row==0) {
        cell.imageView.image = [UIImage imageNamed:@"21"];
    }
    if (indexPath.section ==1 && indexPath.row ==2) {
        cell.imageView.image =[UIImage imageNamed:@"2"];
    }//向第几组,第几行加图片。
   
   
  //1  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
   
   //2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
   //3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
   
  //4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
   //5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
   
        return cell;//将设置好的cell返回
   
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
   
    return view;//自定义每组头视图
   
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}

//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        return 100;
    }
    return 30;
   
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSLog(@"%zi",section);
    return [NSString stringWithFormat:@"%zi",section];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSMutableArray    *_array;//盛放要显示的东西
   
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor grayColor];
    button.frame = CGRectMake(300, 20, 60, 60);
    [button setTitle:@"+" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
    NSLog(@"=====");
    [_array addObject:@"123"];
    [_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
   // cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
   
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor blueColor];
    cell.selectedBackgroundView = view;//自定义cell选中背景
   
    cell.textLabel.text = _array[indexPath.row];

cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
   
   // cell.accessoryView  自定义右侧视图
   
    return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
   
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"----%zi",indexPath.row);
}

//cell被选中 //  点击cell时触发一个方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
NSLog(@"----%zi",indexPath.row);
   
    [tableView
deselectRowAtIndexPath:indexPath animated:YES];//cell被点击时,取消选中状态

}

//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//提交修改动作:  再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [_array removeObjectAtIndex:indexPath.row];
   
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell(有bug 解决bug用一下 1 2 3)

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.dataSource = self;
    [self.view addSubview:table];
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   //1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识

static NSString *identifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    cell.textLabel.text = nil;//3 先清理,在使用
   
    if (indexPath.row == 1) {
        cell.textLabel.text = @"1212";
    }
   
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//1。创建  两种格式UITableViewStylePlain和UITableViewStyleGrouped,其本质上没多大区别,主要是界面风格
    UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //2。行高
    table.rowHeight  = 100;
   
    //3设置分割线的样式风格//很多种自己选择
    table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //4设置分割线的颜色
    table.separatorColor = [UIColor blackColor];
    //5给table添加背景 可以不设置背景的frame
      //5.1第一种: UIView *view =[[UIView alloc]init];
            //view.backgroundColor = [UIColor grayColor];
    //5.2第二种
    UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
    table.backgroundView = view;
    //6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    view2.backgroundColor = [UIColor clearColor];
    table.tableHeaderView = view2;
    //7设置代理
    table.delegate =self;
    //8设置数据源
    table.dataSource = self;
   
    table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
   
    [self.view addSubview:table];
    // Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else if (section ==1){
        return 5;
    }else if(section ==2){
        return 6;
    }
   
    return  4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
    static NSString *identifier = @"cell";//重用机制标识
    UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
                //cell的四种样式
                //UITableViewCellStyleDefault,只显示图片和标题
                //UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
                //UITableViewCellStyleValue2,标题和子标题
               
                //UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
            reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
    }
    cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
    cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
   
      //cell.imageView.image = [UIImage imageNamed:@"012"
                              //];//给每组每行添加图片
   
    cell.detailTextLabel.text [email protected] "who are you";//设置子标题
    if (indexPath.section==0 &&indexPath.row==0) {
        cell.imageView.image = [UIImage imageNamed:@"21"];
    }
    if (indexPath.section ==1 && indexPath.row ==2) {
        cell.imageView.image =[UIImage imageNamed:@"2"];
    }//向第几组,第几行加图片。
   
   
  //1  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
   
   //2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
   //3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
   
  //4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
   //5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
   
        return cell;//将设置好的cell返回
   
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
   
    return view;//自定义每组头视图
   
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}

//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        return 100;
    }
    return 30;
   
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSLog(@"%zi",section);
    return [NSString stringWithFormat:@"%zi",section];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSMutableArray    *_array;//盛放要显示的东西
   
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor grayColor];
    button.frame = CGRectMake(300, 20, 60, 60);
    [button setTitle:@"+" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
    NSLog(@"=====");
    [_array addObject:@"123"];
    [_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
   // cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
   
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor blueColor];
    cell.selectedBackgroundView = view;//自定义cell选中背景
   
    cell.textLabel.text = _array[indexPath.row];

cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
   
   // cell.accessoryView  自定义右侧视图
   
    return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
   
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"----%zi",indexPath.row);
}

//cell被选中 //  点击cell时触发一个方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
NSLog(@"----%zi",indexPath.row);
   
    [tableView
deselectRowAtIndexPath:indexPath animated:YES];//cell被点击时,取消选中状态

}

//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//提交修改动作:  再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [_array removeObjectAtIndex:indexPath.row];
   
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@end

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell(有bug 解决bug用一下 1 2 3)

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.dataSource = self;
    [self.view addSubview:table];
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   //1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识

static NSString *identifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    cell.textLabel.text = nil;//3 先清理,在使用
   
    if (indexPath.row == 1) {
        cell.textLabel.text = @"1212";
    }
   
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

时间: 2024-07-30 13:52:49

UITableView UITableViewCell的相关文章

UITableView UITableViewCell NSIndexPath

--------------------------------------------------------------------------NSIndexPath------------------------------------------------------------------------- 1:初始化NSIndexPath (来自: Kid) inSection 表示TableView 的分组的 第一组数据. NSIndexPath *indexPath = [NSIn

UITableView总结

http://my.oschina.net/iq19900204/blog/292125 摘要 UITableView uitableview uitableviewcell 目录[-] 1.协议介绍 UITableViewDataSource(11) UITableViewDelegate(常用) 2.刷新 下拉刷新: 上拉刷新 3.搜索 4.重用 自定义cell 不使用重用方法 注册Cell 5.编辑 滑动更多 6.优化 UITableView 总结 UITableView是UIScroll

在iOS中获取UIView的所有层级结构 相关

在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便我们完成复杂的 UI 布局,下面的代码就能很方便的获取某个 UI 控件的所有的层级结构,我们可以用它计算,然后把结果写入到本地磁盘,导出成XML文件,这样我们就可以很直观的看出它内部的细节. /** * 返回传入veiw的所有层级结构 * * @param view 需要获取层级结构的view *

在UIKit中谁才最我们的敌人?

我们在开发iOS的过程中,都在调用系统的类,每个类都有丰富的API,但是在开的中很多类型与API我们是用不着的. 当系统的类满不了我们的需要的时候,我就就需要Custom了.在Custom不要贪途方便什么都放一堆. 看看以下这个表,你发现这些对象在内存的大小.相信还有不少人还是不知道的.单位为Bytes /* 2016-03-04 20:15:55.218 Debug[25079:992455] UIAcceleration---- 402016-03-04 20:15:55.219 Debug

ios开发-2015-07-19

ios开发:多线程(NSBlockOperation.线程锁定NSLock).复习UITableView(UITableViewCell风格:UITableViewCellStyleValue1)

学习IOS开发UI篇--UI知识点总结(一) UIButton/UITextField

UIkit框架下的几个基本控件,UIButton,UITextField,UILabel,UIImageView,UIScrollView,UITableView,UITableViewCell,UIPageControl; 他们的继承关系,UILabel,UIImageView,UIScrollView,UITableViewCell,直接继承自UIView; UIButton,UITextField,UIPageControl,继承自UIControl; UIControl继承自UIView

UITableView 没有数据时不要分割线&#160;&#160;。设置UITableViewCell的宽度,就是点击的时候置灰的范围(每天进步一点点)

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero].这一句话会让UITableView下面的分割线全部消失. 在做公司项目的时候,我看到京东是这样做的,就是点击cell的时候一般填充整个屏幕宽度的cell,但是京东的却不是,是有个范围的,实验了一下.只要在自定义的UITableViewCell加上下面这句代码,就跟京东的一样 - (void)setFrame:(CGRect)frame {     

UITableView属性 自己定义UITableViewCell

UITableView的属性全齐.供大家參考 附:http://www.bubuko.com/infodetail-561085.html //曾经在使用UITableView的时候,总是在cell上自己加Label,遇到cell的accessoryType不同的时候,须要自己调整Label的大小和位置. 后来发现 UITableViewCell中有textLabel和detailTextLabel能够使用,系统配置好了大小位置,能够依据cell的不同Style和大小自己主动调整. //text

【转】UITableView详解(UITableViewCell

原文网址:http://www.kancloud.cn/digest/ios-1/107420 上一节中,我们定义的cell比较单一,只是单调的输入文本和插入图片,但是在实际开发中,有的cell上面有按钮,有的cell上面有滑动控件,有的cell上面有开关选项等等,具体参加下面2个图的对比:          @我们可以通过2种方式来实现自定义,一是利用系统的UITableViewCell(但不推荐,因为开发效率不高),举例:还是在这个关键方法中 (UITableViewCell)tableVi