UITableView编程1:实例
来源:互联网 发布:简述数据预处理的内容 编辑:程序博客网 时间:2024/05/16 08:16
首选创建一个新的项目,并添加一个MainViewController的Class文件
打开MainViewController.h文件
- @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
- @property (nonatomic, retain) NSArray *dataList;
- @property (nonatomic, retain) UITableView *myTableView;
- @end
TableView的委托UITableViewDelegate
如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加
然后打MainViewController.m文件,初始化UItableView并显示在当前窗口
- - (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];
- }
第一种:列表 UITableViewStylePlain
第二种:分组UITableViewStyleGrouped
创建并设置每行显示的内容
- - (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;
- }
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
设置内容缩进
- - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return [indexPath row];
- }
设置cell的行高
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 70;
- }
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([indexPath row] % 2 == 0) {
- cell.backgroundColor = [UIColor blueColor];
- } else {
- cell.backgroundColor = [UIColor greenColor];
- }
- }
当选择指定的cell时,弹出UIAlertView显示选择的内容
- - (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];
- }
滑动选择的行后删除
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSLog(@"执行删除操作");
- }
0 0
- UITableView编程1:实例
- UITableView编程1:UITableViewController简介
- UITableView实例详解
- UITableView简单实例*********--------*********
- UI020---UITableView实例汽车品牌
- UITableView的用法实例
- swift3.0 UITableView实例
- IOS编程 之 UITableView
- ACE实例编程1
- ACE实例编程1
- linux编程实例1
- Python编程实例1
- UITableView - 1
- Swift编程 UI控件- UITableView
- iOS编程-------UITableViewEdit_move -----> UITableView 编辑
- Servlet编程实例 续1
- IOS UITableView 可删除行实例
- IOS UITableView 可移动行实例
- Dynamics CRM 2015 New Feature (2): Hierarchical Data
- POJ 3641 - Pseudoprime numbers(快速幂)
- 【HDOJ 1003】 Max Sum
- java之类型转换
- 约瑟夫环
- UITableView编程1:实例
- 【Uni2D】Uni2DEditorTextureAtlasPacker
- HDU 2619 - Love you Ten thousand years (数论)
- 车牌号
- iOS开发 UI Button基础
- QT SQLite 多数据库操作大全
- Tree Cutting (树形DP)
- 一道位运算技巧题
- SDUT 2886 - Weighted Median (思维)