码迷,mamicode.com
首页 > 移动开发 > 详细

Xcode6 IOS开发UITableView基于Storyboard的使用

时间:2015-07-31 01:29:14      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:ios uitableview   uitableview使用   storyboard建uitablevi   可视化创建uitableview   

1.点击项目里面的Storyboard文件,然后给当前的UIViewController控制器里面拖入TableView,如下图所示

技术分享

2.然后选中tableView,选择属性里面的Prototype Cells,设置参数为1,然后设置Style为Group,就会出现一个Cell空间在TableView上,然后给里面拖入你想要的东西,我再这里加入拖入Label

3.给TableView设置代理

设置代理有两种方式,

1)连线:按住control连接tableView到当前的控制器ViewController上

2)代码设置:在需要代理的ViewController.m文件里面加入

self.tableView.delegate = self;
self.tableView.dataSource = self;
设置完之后再把TableView拖到ViewController.h文件里面设置关联
4.自定义Cell

新建SimpleTableViewCell.m文件,然后选中之前的Storyboard里面的Cell,给它添加class设置关联,之后就可以把cell里面的label拖到SimpleTableView.h文件里面

5.在ViewController里面需要实现的TableView先关的

1)给ViewController.m文件@interface后面加入代码如下

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
2)常用的方法

/******************UITableView********************************/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark 一组里面有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"一共是%ld行",tableData.count);
    return tableData.count;
}

#pragma mark indexPath里面对应的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"tableview加载数据中。。。。。。");
    
    Person *person = tableData[indexPath.row];
    static NSString *simpleTableIdentifier = @"SimpleTableViewCell";
    SimpleTableViewCell *cell = (SimpleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    cell.name.text = person.personName;
    cell.describe.text = person.personDescribe;
    cell.icon.image = [UIImage imageNamed:person.personIcon];

    return cell;
}

#pragma mark 返回indexPath这行的cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72;
}


#pragma mark 点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了这里");
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

#pragma mark 左滑动可以选择删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableData removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}


/******************UITableView*******END**********************/

其中Person是定义的一个类,里面包含personName,personDescribe,personIcon等三个属性,然后定义了一个类初始化方法(类似于构造函数)

+(Person*)initPersoneWithName:(NSString*)name Describe:(NSString*)describe Icon:(NSString*)icon;

还有个需要解释的类是SimpleTableViewCell类,它是继承自UITableViewCell的,可以用它来自定义TableView里面的数据的

显示,主要代码如下:.h文件

//
//  SimpleTableViewCell.h
//  PishumTableView
//
//  Created by Pishum on 15/7/29.
//  Copyright (c) 2015年 Pishum. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SimpleTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *describe;
@property (strong, nonatomic) IBOutlet UIImageView *icon;


@end

.m文件

//
//  SimpleTableViewCell.m
//  PishumTableView
//
//  Created by Pishum on 15/7/29.
//  Copyright (c) 2015年 Pishum. All rights reserved.
//

#import "SimpleTableViewCell.h"

@implementation SimpleTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

需要提醒的是,在使用Storyboard的时候,在上面的2步骤中,设置TableView的cell时,需要给关联上刚才定义的

SimpleTableViewCell类,而且还要在Storyboard里给它设置identifier,在这里我设置的identifier是"SimpleTableViewCell"和

类名相同,这样可以避免忘记。

只要给tableData里面添加数据就好了,这里有个简单的添加例子,当然了方法有很多,在这里不多说了

-(NSMutableArray*)getTableData{
    NSMutableArray * array = [[NSMutableArray alloc]init];
    Person *person1 =[Person initPersoneWithName:@"刘备" Describe:@"双股剑" Icon:@"switch.png"];
    Person *person2 =[Person initPersoneWithName:@"关羽" Describe:@"青龙偃月刀" Icon:@"switch.png"];
    Person *person3 =[Person initPersoneWithName:@"张飞" Describe:@"丈八蛇矛" Icon:@"switch.png"];
    [array addObject:person1];
    [array addObject:person2];
    [array addObject:person3];
    
    return array;
}


源码下载地址

https://github.com/pishum/PishumTableView


本文原创,转载请注明出处

版权声明:本文为博主原创文章,未经博主允许不得转载。

Xcode6 IOS开发UITableView基于Storyboard的使用

标签:ios uitableview   uitableview使用   storyboard建uitablevi   可视化创建uitableview   

原文地址:http://blog.csdn.net/pishum/article/details/47137685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!