UITableView的常用方法,属性

示例代码(一共4份)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据
UITableViewDataSource
>

{
    //定义一个数据视图对象
    //数据视图是用来显示大量相同格式的信息的视图
    //例如:电话通讯录,QQ好友,朋友圈
    UITableView* _tableView;
}


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //创建数据视图
    //P1:数据视图的位置
    //P2:数据视图的风格
    //Plain:普通风格
    //Grouped:分组风格
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];


    //设置数据视图的对象
    _tableView.delegate = self;
    //设置数据视图的数据源对象
    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

}

//获取每组元素的行数
//必须要实现的协议函数
//程序在显示数据视图时会调用此函数
//返回值:表示每组元素的个数
//P1:数据视图对象本身
//P2:那一组需要的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

//设置数据视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//创建单元格对象函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* cellStr = @"cell";

    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];

    if (cell == nil) {
        //创建一个单元格对象
        //P1:单元格样式
        //P2:单元格的复用标记
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }

    NSString* str = [NSString stringWithFormat:@"第%ld组, 第%ld行!", indexPath.section, indexPath.row];

    //将单元格的主文字内容赋值
    cell.textLabel.text = str;

    return cell;

}


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


@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<
UITableViewDataSource,
UITableViewDelegate
>

{
    UITableView* _tableview;

    NSMutableArray* _arrayData;
}

@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 536) style:UITableViewStyleGrouped];

    _tableview.delegate = self;
    _tableview.dataSource = self;

    //数据视图显示
    [self.view addSubview:_tableview];

    //创建一个可变数组
    _arrayData = [[NSMutableArray alloc] init];

    for (int i = 'A'; i <= 'Z'; i++) {
        //定义小数组:就是二维数组的每一行
        NSMutableArray* arraySmall = [[NSMutableArray alloc] init];

        for (int j = 1; j <= 5; j++) {
            NSString* str = [NSString stringWithFormat:@"%c%d", i, j];

            [arraySmall addObject:str];
        }

        //生成一个二维数组
        [_arrayData addObject:arraySmall];
    }

    NSLog(@"a = %d", _arrayData.count);

}

//获取组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _arrayData.count;
}

//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numRow = [[_arrayData objectAtIndex:section] count];

    return numRow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* str = @"cell";

    UITableViewCell* cell = [_tableview dequeueReusableCellWithIdentifier:str];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }

    cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];

    return cell;

}

//获取高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//获取每组头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"头部标题";
}

//获取每组尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"尾部标题";
}

//获取尾部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 60;
}

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


@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

<
UITableViewDelegate,
UITableViewDataSource
>

{
    UITableView* _tabelView;
    NSMutableArray* _arrayData;

    //添加导航按钮
    UIBarButtonItem* _btnEdit;
    UIBarButtonItem* _btnFinsh;
    UIBarButtonItem* _btnDelete;
    //设置编辑状态
    BOOL _isEdit;
}

@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    //自动调整子视图大小
    _tabelView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
    UIViewAutoresizingFlexibleWidth;

    //设置代理
    _tabelView.delegate = self;
    _tabelView.dataSource = self;

    //数据视图的头部视图的设定
    _tabelView.tableHeaderView = nil;
    //数据视图的尾部视图
    _tabelView.tableFooterView = nil;

    [self.view addSubview:_tabelView];

    //初始化数据源数组
    _arrayData = [[NSMutableArray alloc] init];

    for (int i = 1; i < 20; i++) {
        NSString* str = [NSString stringWithFormat:@"A %d", i];

        [_arrayData addObject:str];
    }

    //当数据源发生变化
    //更新数据视图,重新加载数据
    [_tabelView reloadData];

    [self createBtn];
}

- (void) createBtn
{
    _isEdit = NO;

    //
    _btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
    _btnFinsh = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinsh)];
    _btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];

    self.navigationItem.rightBarButtonItem = _btnEdit;
}

- (void) pressEdit
{
    _isEdit = YES;
    self.navigationItem.rightBarButtonItem = _btnFinsh;
    [_tabelView setEditing:YES];
    self.navigationItem.leftBarButtonItem = _btnDelete;

}

- (void) pressFinsh
{
    _isEdit = NO;
    self.navigationItem.rightBarButtonItem = _btnEdit;
    [_tabelView setEditing:NO];
    self.navigationItem.leftBarButtonItem = nil;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //行数就是几个数据在数组里
    return _arrayData.count;
}

//默认返回1
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* strID = @"ID";

    //尝试获取可以复用的单元格
    //如果得不到,返回为nil
    UITableViewCell* cell = [_tabelView dequeueReusableCellWithIdentifier:strID];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
    }

    //单元格文字赋值
    cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];

    return cell;
}



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


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _tabelView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    //自动调整子视图大小
    _tabelView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
    UIViewAutoresizingFlexibleWidth;

    //设置代理
    _tabelView.delegate = self;
    _tabelView.dataSource = self;

    //数据视图的头部视图的设定
    _tabelView.tableHeaderView = nil;
    //数据视图的尾部视图
    _tabelView.tableFooterView = nil;

    [self.view addSubview:_tabelView];

    //初始化数据源数组
    _arrayData = [[NSMutableArray alloc] init];

    for (int i = 1; i < 20; i++) {
        NSString* str = [NSString stringWithFormat:@"A %d", i];

        [_arrayData addObject:str];
    }

    //当数据源发生变化
    //更新数据视图,重新加载数据
    [_tabelView reloadData];

    [self createBtn];
}

- (void) createBtn
{
    _isEdit = NO;

    //
    _btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(pressEdit)];
    _btnFinsh = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(pressFinsh)];
    _btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(pressDelete)];

    self.navigationItem.rightBarButtonItem = _btnEdit;
}

//可以显示编辑状态,当手指在单元格上移动时
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除对应的数据
    [_arrayData removeObjectAtIndex:indexPath.row];

    //数据源更新
    [_tabelView reloadData];



}

//选中单元格调用此协议函数
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选中单元格!%d, %d", indexPath.section, indexPath.row);
}

//取消选中时调用的协议函数
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消选中单元格!%d, %d", indexPath.section, indexPath.row);
}



//单元格显示效果协议
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //默认是删除 UITableViewCellEditingStyleDelete
    //插入 UITableViewCellEditingStyleInsert
    //全没 UITableViewCellEditingStyleNone
    //多选 UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
    return UITableViewCellEditingStyleDelete;
}

- (void) pressEdit
{
    _isEdit = YES;
    self.navigationItem.rightBarButtonItem = _btnFinsh;
    [_tabelView setEditing:YES];
    self.navigationItem.leftBarButtonItem = _btnDelete;

}

- (void) pressFinsh
{
    _isEdit = NO;
    self.navigationItem.rightBarButtonItem = _btnEdit;
    [_tabelView setEditing:NO];
    self.navigationItem.leftBarButtonItem = nil;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //行数就是几个数据在数组里
    return _arrayData.count;
}

//默认返回1
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* strID = @"ID";

    //尝试获取可以复用的单元格
    //如果得不到,返回为nil
    UITableViewCell* cell = [_tabelView dequeueReusableCellWithIdentifier:strID];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
    }

    //单元格文字赋值
    cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
    //设置文字标题
    cell.detailTextLabel.text = @"子标题";

    NSString* str = [NSString stringWithFormat:@"%d.jpg", indexPath.row % 7 + 1];

    UIImage* image = [UIImage imageNamed:str];

    //IImageView* iView = [[UIImageView alloc] initWithImage:image];
    //设置默认图标信息
    cell.imageView.image = image;

    return cell;
}

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

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


@end

心得体会

  1. UITableViewDelegate,UITableViewDataSource这两个协议必须带上
  2. 2.
//设置需要多少组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

}

//设置数据视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

}

//创建单元格对象函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

}
这三个函数必须要有
  1. 数据源一般是个NSMutableArray
  2. 根据行数找出该有的那个
  3. 数据的删除要分成两部分,第一步先删除数据源部分,第二步更新数据

猜你喜欢

转载自blog.csdn.net/KevinAshen/article/details/81167913
今日推荐