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*)initPersoneWithName:(NSString*)name Describe:(NSString*)describe Icon:(NSString*)icon;
显示,主要代码如下:.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
//
// 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
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的使用
原文:http://blog.csdn.net/pishum/article/details/47137685