IOS控件UITableView详解
2015-12-03 16:52
423 查看
终于写到了UITableView,用处最广的一个控件,当然也是要记相当多东西的一个控件。
首选创建一个新的项目,并添加一个MainViewController的Class文件
![](http://img.my.csdn.net/uploads/201210/11/1349966518_1905.png)
打开MainViewController.h文件
[cpp] view
plaincopy
@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSArray *dataList;
@property (nonatomic, retain) UITableView *myTableView;
@end
TableView的数据源UITableViewDataSource
TableView的委托UITableViewDelegate
如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加
然后打MainViewController.m文件,初始化UItableView并显示在当前窗口
[cpp] view
plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
// 初始化tableView的数据
NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil];
self.dataList = list;
UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease];
// 设置tableView的数据源
tableView.dataSource = self;
// 设置tableView的委托
tableView.delegate = self;
// 设置tableView的背景图
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
self.myTableView = tableView;
[self.view addSubview:myTableView];
}
在初始化的时候,可以为TableView设置样式
第一种:列表 UITableViewStylePlain
![](http://img.my.csdn.net/uploads/201210/11/1349966988_9179.png)
第二种:分组UITableViewStyleGrouped
![](http://img.my.csdn.net/uploads/201210/11/1349967034_3861.png)
创建并设置每行显示的内容
[cpp] view
plaincopy
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellWithIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.dataList objectAtIndex:row];
cell.imageView.image = [UIImage imageNamed:@"green.png"];
cell.detailTextLabel.text = @"详细信息";
return cell;
}
UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式
UITableViewCellStyleDefault
![](http://img.my.csdn.net/uploads/201210/11/1349967817_1966.png)
UITableViewCellStyleSubtitle
![](http://img.my.csdn.net/uploads/201210/11/1349967869_2709.png)
UITableViewCellStyleValue1
![](http://img.my.csdn.net/uploads/201210/11/1349967918_2001.png)
UITableViewCellStyleValue2
![](http://img.my.csdn.net/uploads/201210/11/1349967950_1118.png)
分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段
[cpp] view
plaincopy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
设置内容缩进
[cpp] view
plaincopy
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row];
}
![](http://img.my.csdn.net/uploads/201210/11/1349967242_8766.png)
设置cell的行高
[cpp] view
plaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
设置cell的隔行换色
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % 2 == 0) {
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor greenColor];
}
}
![](http://img.my.csdn.net/uploads/201210/11/1349967395_3275.png)
当选择指定的cell时,弹出UIAlertView显示选择的内容
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[msg release];
[alert show];
}
![](http://img.my.csdn.net/uploads/201210/11/1349967479_8450.png)
滑动选择的行后删除
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"执行删除操作");
}
首选创建一个新的项目,并添加一个MainViewController的Class文件
![](http://img.my.csdn.net/uploads/201210/11/1349966518_1905.png)
打开MainViewController.h文件
[cpp] view
plaincopy
@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSArray *dataList;
@property (nonatomic, retain) UITableView *myTableView;
@end
TableView的数据源UITableViewDataSource
TableView的委托UITableViewDelegate
如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加
然后打MainViewController.m文件,初始化UItableView并显示在当前窗口
[cpp] view
plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
// 初始化tableView的数据
NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil];
self.dataList = list;
UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease];
// 设置tableView的数据源
tableView.dataSource = self;
// 设置tableView的委托
tableView.delegate = self;
// 设置tableView的背景图
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
self.myTableView = tableView;
[self.view addSubview:myTableView];
}
在初始化的时候,可以为TableView设置样式
第一种:列表 UITableViewStylePlain
![](http://img.my.csdn.net/uploads/201210/11/1349966988_9179.png)
第二种:分组UITableViewStyleGrouped
![](http://img.my.csdn.net/uploads/201210/11/1349967034_3861.png)
创建并设置每行显示的内容
[cpp] view
plaincopy
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellWithIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.dataList objectAtIndex:row];
cell.imageView.image = [UIImage imageNamed:@"green.png"];
cell.detailTextLabel.text = @"详细信息";
return cell;
}
UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式
UITableViewCellStyleDefault
![](http://img.my.csdn.net/uploads/201210/11/1349967817_1966.png)
UITableViewCellStyleSubtitle
![](http://img.my.csdn.net/uploads/201210/11/1349967869_2709.png)
UITableViewCellStyleValue1
![](http://img.my.csdn.net/uploads/201210/11/1349967918_2001.png)
UITableViewCellStyleValue2
![](http://img.my.csdn.net/uploads/201210/11/1349967950_1118.png)
分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段
[cpp] view
plaincopy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
设置内容缩进
[cpp] view
plaincopy
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row];
}
![](http://img.my.csdn.net/uploads/201210/11/1349967242_8766.png)
设置cell的行高
[cpp] view
plaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
设置cell的隔行换色
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % 2 == 0) {
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor greenColor];
}
}
![](http://img.my.csdn.net/uploads/201210/11/1349967395_3275.png)
当选择指定的cell时,弹出UIAlertView显示选择的内容
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[msg release];
[alert show];
}
![](http://img.my.csdn.net/uploads/201210/11/1349967479_8450.png)
滑动选择的行后删除
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"执行删除操作");
}
![](http://img.my.csdn.net/uploads/201210/11/1349969102_4981.png)
相关文章推荐
- UITextField 总结
- iOS8中用UIVisualEffectView实现高斯模糊视图(毛玻璃效果)
- 选择移动web开发框架研究——有mui、frozenui以及Sencha Touch等
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
- UITextField输入到最大限值以后不能删除
- 连接数据库超时设置autoReconnect=true
- [leetcode] 95. Unique Binary Search Trees II
- Android-Mac电脑如何进行APK反编译-使用apktool、jd-gui
- 去掉UISearchBar边框
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- easy-ui JOB 及 小记录
- 自定义Drawable(文字按钮点击效果设置)
- IOS上 关于状态栏的相关设置(UIStatusBar)
- iOS开发之键盘类型UIKeyboardType
- 招标系统-easyui插件中标签选择和创建
- UIGestureRecognizer手势
- easyui学习笔记
- cuda学习笔记——deviceQuery
- UITouch 触摸
- UITouch 触摸