UI—UITableView

一、UITableView


1、UITableView是UIScrollView的子类,具有滚动功能。
2、UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等,没有设置数据源的UITableView只是个空壳。
3、凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源。
4、通常都要为UITableView设置代理对象(delegate),以便在UITableView触发一下事件时做出相应的处理,比如选中了某一行。
5、凡是遵守了UITableViewDelegate协议的OC对象,都可以是UITableView的代理对象一般会让控制器充当UITableViewdataSourcedelegate
6、用字典重构数据模型
@interfaceViewController ()<UITableViewDataSource>实现代理 一般要实现数据源方法和代理方法
{
   NSArray *_dictionaryINArray;//在数组里面装字典,在这里神明的全局变量具有private属性
}
@end
@implementation ViewController

这种地方写的全局变量属于C语言的全局变量
- (void)viewDidLoad {
    [superviewDidLoad];
    UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];手动添加一个控件,设置好控件大小。同时设置好样式,有两种一种是分组的grouped一种是不分组的plain。
    tableView.dataSource =self;设置数据源 数据源的设置有两种方法,一种是拖线但控件也是需要脱险的
    [self.viewaddSubview:tableView];
数据,当用到字典的时候最好用模型,面向对象思想编程。假数据
    _dictionaryINArray =@[
                          @{@"header":@"吉林",
                            @"footer":@"很好",
                            @"cities":@[@"长春",@"农安",@"四平"]
                            },
                          @{@"header":@"吉林",
                            @"footer":@"很好",
                            @"cities":@[@"长春",@"农安",@"四平",@"延吉"]
                            },
                          ];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return_dictionaryINArray.count;
}
//有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   NSDictionary *dict =_dictionaryINArray[section];
   NSArray *city = dict[@"cities"];
   return city.count;
}
//每一行都显示什么数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];创建UItableViewCell
   NSDictionary *dict =_dictionaryINArray[indexPath.section];
   NSArray *array = dict[@"cities"];
   NSString *text = array[indexPath.row];
    cell.textLabel.text = text;给cell里面的UILable文本赋值
   return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return_dictionaryINArray[section][@"header];设置头标题
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{设置尾标题
   return_dictionaryINArray[section][@"footer];//这样的联系写就不能用点调用对象特有的方法,编译器识别不了。
}
@end

6、用模型重构数据模型,UI空间用weak,字符串用copy,对象用strong,只需要把上面代码里的字典变成对象即可。
7、索引条

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//返回的数组即为又边导航栏的内容,数组中的内容无所谓,滚动范围是根据在数组中的位置。
   NSMutableArray *array = [NSMutableArrayarray];
   for (NSDictionary *obj in_dictionaryINArray) {
        [arrayaddObject:obj[@"header"]];
    }
   return array;
}



二、UITableViewCell


重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象

@property (nonatomic,readonly,retain)UIImageView *imageViewNS_AVAILABLE_IOS(3_0);
@property (nonatomic,readonly,retain)UILabel     *textLabelNS_AVAILABLE_IOS(3_0)
@property (nonatomic,readonly,retain)UILabel     *detailTextLabel 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //cell内置了两个UILabel和一个UIImageView,同时提供了四种UITableViewCell样式style,主要为default和subtitle。同时还提供了一个小的图标,cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];这是UITableVIewCell的构造方法,初始化的时候,选区四种样式之一进行设置即可,这种cell的样式都是固定的,如果想自定义后期会学习。
    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;默认两个lable一个imageVIew 一个小图标。
    return cell;
}


三、UITableView单组数据展示 弹框显示商品名称 刷新表格数据 局部刷新


1在storyBoard中拖一个UITableView,style有两种grouped和plain两种方式。要个这个UITableView设置数据源有两种方法,一种是拖线,另一种可以把UI当作属性,然后通过成员变量进行设置控制器为数据源。当UITableView里面的cell被电击后,应该调用代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{代理方法,当选中了UITableVIew内的某一行Cell就会调用如下代码,想要接受用户输入,则需要一个文本框,UIAlertVIew自带文本输入样式,只需设置就好。
    NSDictionary *dict = _dictionaryINArray[indexPath.section];取出数据一般是模型,这里不要管,记得是数据就行。
    
    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:dict[@"header"]message:dict[@"footer"]delegate:selfcancelButtonTitle:@"cancel"otherButtonTitles:@"sure",nil];创建一个UIAlertView控件,设置标题头,描述信息,代理对象,取消键值,其他按钮可以多个。
    alert.alertViewStyle =UIAlertViewStylePlainTextInput;最重要的是这行,给UI设置样式,都是实现好的,有课输入文本的一个样式接受用户输入
    [alert textFieldAtIndex:0].text = dict[@"footer];取得如上所说的文本框。第o个就是,如果是密码框则会有两个文本输入框。
    [alert show];想让空间展示只需show就可以,无需加入什么父控件。
}


2、刷新表格数据在上面的方法里可以弹出对话框,并通过indexPath找到对应的数据展示到文本框中,但还需要把数据设置会去。通过如下代码。
UIAlertVIew也有代理方法。UIAlertViewDelegate.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex ==0)return;//这里面是button按钮的值
    if (buttonIndex ==1) {
        NSString *text =[alertViewtextFieldAtIndex:0].text;//调用默哥对象的代理方法是一般都会将自己传进来,这取的文本框的值,textFieldAtIndex
       //修改对应位置的模型数据修改,但需要indexPath,弹出对话框的那个放法里有didSelectRowAtIndexPath,找到两个方法之间的桥梁,alertview通过tag来进行沟通,但职能设置一个值,单组的可以,多组的就需要全局变量,在方法外面写,整个程序运行期间都会存在
        NSString *str =_dictionaryINArray[0][@"header"];//加数据默认是分组的tableView
        [_tableVIewreloadData];//talbeView重新加载cell,先数据源发送信息。整个重新加载
    }
}


3 局部刷新

用下面代码代替 [_tableVIewreloadData]即可实现局部刷新。
        NSIndexPath *path = [NSIndexPathindexPathForRow:1inSection:0];要刷新哪一组哪一行
        NSArray *array = @[path];把要刷新的indexpath放到数组中去
        [_tableVIewreloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationBottom];只刷新指定的indexpath那一行,并动画


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值