UITableView常用方法

UITableView初认识

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

    //UITableView 继承于 UIScrollView
    //UITableViewStylePlain默认样式
    //UITableViewStyleGrouped 自动分段
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    //设置代理
    //外观
    tableView.delegate = self;
    //数据
    tableView.dataSource = self;

    [self.view addSubview:tableView];

}

//pragma mark – UITableViewDelegate

//设置段 默认是返回是1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

//设置一段有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 2;
}
return 10;
}

//设置cell
//cell返回是根据段和行来确定
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath 有一个类别专门为UITableView服务的
//indexPath 类别中有两个属性 section 代表tabbleView的段 row代表tabbleView的行

// NSLog(@”第%ld行”,indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//设置副标题
cell.detailTextLabel.text = @”副标题”;
//设置图片
cell.imageView.image = [UIImage imageNamed:@”001@2x”];
//设置主标题
cell.textLabel.text = [NSString stringWithFormat:@”第%ld段,第%ld行”,indexPath.section,indexPath.row];

//cell上面还有一层视图 contentView和cell是一样大小
cell.contentView.backgroundColor = [UIColor redColor];

return cell;

}

//设置cell的行高 系统默认是44
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 100;
}

UITableView常用方法

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

//编辑按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@”Edit” style:UIBarButtonItemStylePlain target:self action:@selector(editing:)];

//滚动按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.tintColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 50, 30);
[button setTitle:@”滚到底部” forState:UIControlStateNormal];
[button addTarget:self action:@selector(goBottom) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;

//刷新tabbleView
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@”刷新” style:UIBarButtonItemStylePlain target:self action:@selector(refresh)];

//创建tableview
[self createTableView];

//加载数据源
[self loadData];
}

  • (void)refresh
    {
    [_tableView reloadData];
    }

  • (void)goBottom
    {

    //滚到底部
    //数组的长度减去1 代表最后一个元素 也代表tabbleView中最后一行
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.count - 1 inSection:0];

    //滚到指定行
    [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }

  • (void)createTableView
    {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];

    _tableView.delegate = self;
    _tableView.dataSource = self;

    [self.view addSubview:_tableView];
    }

  • (void)loadData
    {
    _dataArr = [NSMutableArray arrayWithObjects:@”北京”,@”上海”,@”广州”,@”深圳”,@”大连”,@”郑州”,@”成都”,@”长沙”,@”重庆”,@”武汉”,@”新加坡”, nil];
    }

//编辑状态
- (void)editing:(UIBarButtonItem *)item
{
_tableView.editing = !_tableView.editing;
if (_tableView.editing) {
item.title = @”Done”;
}else
{
item.title = @”Edit”;
}
}

//pragma mark — UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *signCell = @”cell”;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:signCell];

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

    //系统自带的按钮样式
    cell.accessoryType = UITableViewCellAccessoryDetailButton;

    cell.textLabel.text = _dataArr[indexPath.row];

    return cell;
    }

//设置cell行高
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 100;
}

//选中指定行(cell)
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
//indexPath.row和数组的下标是一致的
NSLog(@”index == %ld”,indexPath.row);
// NSLog(@”title == %@”,_dataArr[indexPath.row]);
SecondViewController *second = [[SecondViewController alloc] init];
second.name = _dataArr[indexPath.row];
[self.navigationController pushViewController:second animated:YES];
}

//pragma mark — option可选择函数

//处理信息按钮的事件
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”您的余额已不足请及时充值” delegate:nil cancelButtonTitle:@”确定” otherButtonTitles:nil, nil];
[alert show];
}

//定制删除按钮
- (NSString )tableView:(UITableView )tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @”删除”;
}

//指定行编辑
- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == 0) {
return NO;
}
return YES;
}

//删除/插入
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    //1.先删除数据源中的元素
    [_dataArr removeObjectAtIndex:indexPath.row];

    //2.处理UI界面
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}else
{
    //1.先插入到数据源中
    [_dataArr insertObject:@"南极" atIndex:indexPath.row];

    //2.处理UI界面
    [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

}

//设置编辑模式(删除或者增加)
- (UITableViewCellEditingStyle )tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleInsert;
}

//移动cell
- (void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//对数据源中的元素交换位置
//sourceIndexPath 要交换的元素
//destinationIndexPath 目标元素

[_dataArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

}

  • (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
    {
    return _headArr[section];
    }

  • (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
    {
    return _footArr[section];
    }

UITableViewCell复用机制

//如果滚动tabbleView都会触发这个方法
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//复用机制
//1.创建了一个复用标识
static NSString *signCell = @”cell”;

//2.去tabbleView中取带有复用标识的cell
//每个tableview都会有一个复用机制,滚动tableview如果有cell滚出屏幕就会自动加入到tabbleView的队列中,遵循先进先出的原则
//signCell 复用标识
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:signCell];

//3.如果没有带复用标识的cell就重新创建
if (cell == nil) {
    //创建带有标识的cell
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:signCell];
}

//tableview复用的优点:节省系统内存 优化系统程序

NSArray *arr = _arr[indexPath.section];
cell.textLabel.text = arr[indexPath.row];

return cell;

}

//设置cell的行高
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 100;
}

//设置段头标题
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
return _headArr[section];
}

//设置段尾标题
//- (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
//{
// return _headArr[section];
//}

  • (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
    {
    //索引和组数是相对应的,如果索引字符超过当前组数的字母点击是没有响应的
    NSMutableArray *arrM = [NSMutableArray array];
    for(int i = ‘A’; i <= ‘Z’; i++)
    {
    NSString *str = [NSString stringWithFormat:@”%c”,i];
    [arrM addObject:str];
    }
    return arrM;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值