您的位置:首页 > 产品设计 > UI/UE

UITableView的用法实例

2015-10-11 16:04 387 查看
UI中的高级控件 之一   UITableView  现在用的最广泛的控件  下面简单介绍它的用法

1.创建tableView

- (void)createTableView

{

    _tableView = [[UITableView
alloc] init];

    CGSize size =
self.view.frame.size;

    _tableView.frame =
CGRectMake(0,
20, size.width, size.height-20);

    

   
//设置数据源代理

    _tableView.dataSource =
self;

    

    //注册复用标识及cell类型

    [_tableView
registerClass:UITableViewCell.class
forCellReuseIdentifier:@"cellId"];

    

    [self.view
addSubview:_tableView];

}
2.代理的必须实现的方法

//返回指定的区的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return
_dataSource.count;

}

//返回指定的区的指定的行显示的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //static int number = 0;

    

    //tableView通过一个队列来管理所有的cell

   
//目的是为了复用(节约资源,提高效率)

   
//若队列中有空闲的,则直接返回

   
//若队列中没有空闲的,则需要创建新的cell

    UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cellId"];

#if 0

   
//若提前已经注册复用标识及cell类型,则出列时会自行判断

   
//队列中有空闲的直接返回;若没有则会自动创建

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];

        number++;

        NSLog(@"number:%d", number);

    }

#endif

    //cell中有三个视图textLabel/detailLabel/imageView

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

    return cell;

}
3.创建数据源

-(void)creatDataSourse{

    mArr = [[NSMutableArray
alloc]init];

    _dataSourse = [[NSMutableArray
alloc]init];

    NSArray *arr = [[NSBundle
mainBundle]pathsForResourcesOfType:@"plist"
inDirectory:nil];

    for (NSString *plist
in arr) {

        NSRange range = [plist
rangeOfString:@"/"
options:NSBackwardsSearch];

        NSString *name = [plist
substringWithRange:NSMakeRange(range.location+1,
4)];

        //过滤Info.plist

        //NSLog(@"%@",name);

        if ([name
isEqualToString:@"Info"]) {

            continue;

        }

       // NSLog(@"%@",name);

        _imageNames = [[NSMutableArray
alloc]init];

        NSArray *plistInfo = [[NSArray
alloc]initWithContentsOfFile:plist];

        for (NSDictionary *dic
in plistInfo) {

            NSString *imageName =dic[@"imageName"];

            [_imageNames
addObject:imageName];

            //NSLog(@"%@",imageName);

        }

        //NSLog(@"%@",_imageNames);

        [mArr
addObject:_imageNames];

        

        name = [name stringByAppendingFormat:@"(%lu)",_imageNames.count];

        [_dataSourse
addObject:name];

    

    }

    NSLog(@"%@",mArr);

    

}
4.点击tabelView实现的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TileViewController *til= [[TileViewController
alloc]initWithImageNames:mArr[indexPath.row]];

    //NSLog(@"%@",mArr);

    

    [self.navigationController
pushViewController:til animated:YES];

}
5.在图片类中创建图片  

-(instancetype)initWithImageNames:(NSArray *)names{

    if (self=  [super
init]) {

       
//根据名字
创建图片 并贴到视图控制器上

        _imageNames = [[NSMutableArray
alloc]initWithArray:names];

        _scrollView = [[UIScrollView
alloc]init];

       // NSLog(@"%@ %lu",names,names.count);

        //NSLog(@"%lu",_imageNames.count);

        for (NSString *str
in _imageNames) {

            

            NSString *name = [str
substringToIndex:4];

            self.title = name;

            

        }

    }

    return
self;

}
6.设置滚动视图

-(void)setSrollerView{

    self.automaticallyAdjustsScrollViewInsets =
NO;

    CGSize size =
self.view.frame.size;

    _scrollView.frame =
CGRectMake(0,
64, size.width,size.height-64);

    _scrollView.bounces =
NO;

   

    [self
setImageToScrellerView];

    [self.view
addSubview:_scrollView];

    

}
7.加载图片内容

-(void)setImageToScrellerView{

  

        CGSize size =
_scrollView.frame.size;

        CGFloat gap =
5;

        CGFloat width = (size.width-5*gap)/4;

        CGFloat height = width*3/2.0;

        NSInteger rows = (_imageNames.count+3)/4;

        for (NSInteger i =
0; i<rows; i++) {

            for (NSInteger j =
0; j<4; j++) {

                //计算本次要贴的图片在数组中的下标

                NSInteger index =
4*i+j;

                if (index >=
_imageNames.count) {

                    break;

                }

                CGRect frame =
CGRectMake(gap+(gap+width)*j, gap+(gap+height)*i, width, height);

                UIImageView *view = [[UIImageView
alloc]initWithFrame:frame];

                view.tag = index;

                view.image = [UIImage
imageNamed:_imageNames[index]];

                //添加单击手势

                view.userInteractionEnabled =
YES;

                UITapGestureRecognizer *tgr = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tagHandle:)];

                [view addGestureRecognizer:tgr];

                [tgr release];

                

                [_scrollView
addSubview:view];

                [view release];

            

        }

         _scrollView.contentSize =
CGSizeMake(size.width, rows*(height+gap)+gap);

        

    }

    

}
8.设置单击手势方法

- (void)tagHandle:(UITapGestureRecognizer *)tgr

{

    SkimViewController *svc = [[SkimViewController
alloc]
initWithImageNames:_imageNames
index:tgr.view.tag];

    svc.hidesBottomBarWhenPushed =
YES;

    [self.navigationController
pushViewController:svc animated:YES];

    [svc release];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息