ios开发笔记(二)—— UITableView的常用用法

相关代理方法

UITableViewDataSource ,UITableViewDelegate,UIAlertViewDelegate

#pragma mark UITableViewDataSource相关方法
/**
 *  这个方法是设置tableView中组的个数的方法。
    默认返回是1
 *
 *  @param tableView tableView
 *
 *  @return 组的个数
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**
 *  这个方法是设置各个组中的行数的方法
 *
 *  @param tableView tableView
 *  @param section   section代表的就是组的编号,具体可以根据组的编号来设置行数
 *
 *  @return 每个组的行数
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_shops count];
}

/**
 *  这个方法是设置每行数据显示的内容的方法,
 *  
 *  每个cell都包含几个基本元素(一个UIImageView 两个UILabel)
 *      cell.imageView.image = [UIImage imageNamed:imageName];
 *      cell.textLabel.text = textName;
 *      cell.detailTextLabel.text = textName;
 *  如果要显示出detailTextLable则需要修改其样式 (具体看方法体内的注释)
 *
 *  设置cell右边的view
 *  cell.accessoryType (具体看方法体内的注释)
 *
 *  @param tableView tableView
 *  @param indexPath indexPath 表示行数的数字编号,默认从0开始
 *
 *  @return 返回一个cell对象,在视图中显示出来
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  UITableViewCellStyleDefault : 不显示detailTextLabel
     *  UITableViewCellStyleValue1  :detailTextLabel显示,但是是在textLabel右边显示
     *  UITableViewCellStyleValue2  :detailTextLabel显示,但是textLabel比较小,detailTextLabel比较大,而且不支持显示图片
     *  UITableViewCellStyleSubtitle:detailTextLabel显示,textLabel在第一行,detailTextLabel在第二行,而且支持显示图片
     */
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    XYShop *shop = _shops[indexPath.row];
    
    cell.textLabel.text = shop.name;
    cell.detailTextLabel.text = shop.desc;
    cell.imageView.image = [UIImage imageNamed:shop.icon];
    /**
     *  UITableViewCellAccessoryNone 不显示任何右边的视图
     *  UITableViewCellAccessoryDisclosureIndicator   在右边显示一个小箭头
     *  UITableViewCellAccessoryDetailDisclosureButton 在右边显示一个详情按钮
     *  UITableViewCellAccessoryCheckmark  在右边显示一个对勾
     *  UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) 在右边显示一个详情按钮
     */
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark UITableViewDelegate代理方法
/**
 *  返回每行的高度
 *
 *  @param tableView tableView
 *  @param indexPath indexPath
 *
 *  @return return 行高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XYShop *shop = _shops[indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"详情显示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    /**
     *  alert.alertViewStyle属性设置枚举说明
     *  UIAlertViewStyleDefault  默认显示样     
     *  UIAlertViewStyleSecureTextInput  一个输入框 可以输入密码样式
     *  UIAlertViewStylePlainTextInput   一个输入框 可以输入默认样式
     *  UIAlertViewStyleLoginAndPasswordInput 一个输入框 可以输入用户名和密码样式
     */
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert textFieldAtIndex:0].text = shop.name;
    alert.tag = indexPath.row;
    [alert show];
}


#pragma mark UIAlertViewDelegate代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) return;
    
    NSString *str = [alertView textFieldAtIndex:0].text;
  
    
    XYShop *shop = _shops[alertView.tag];
    shop.name = str;
    
    //全局刷新
    //[_tableView reloadData];
    
    //局部刷新
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
    NSArray *paths = @[indexPath];
    [_tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
}


总结:


#pragma mark 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 取消选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)
// 只要实现了这个方法,就会默认添加滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当移动了某一行cell就会调用
// 只要实现了这个方法,就会默认添加排序功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

二、修改Cell的状态
1.最好通过“修改模型数据”来修改Cell的状态

2.修改步骤
1> 修改模型数据
2> 刷新表格
* 整体刷新:reloadData(最重要)
* 局部刷新:reloadRowsAtIndexPaths:withRowAnimation:

三、UITableView常见方法
1.取消选中某一行(去掉cell选中时默认的蓝色背景)
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

2.局部刷新(仅仅刷新indexPaths数组中装着的行)
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

3.整体刷新(屏幕中的每一行都刷新)
- (void)reloadData;

4.直接删除界面上的行数(要求模型数据也要删掉对应的数量)
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

5.设置编辑模式
@property(nonatomic,getter=isEditing) BOOL editing; 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 注意:
不管是局部刷新,还是整体刷新,原理都是:
UITableView重新向数据源(dataSource)和代理(delegate)发送相应的消息,最终将得到的数据展示出来


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值