学了ios开发有一段时间了,或许这就是做设计的人学程序的弊端,对于基本的概念一直还是不熟练,今天晚上回来了解了一下UItableView,然后整理一下思路方便后面复习查询,我的第一篇初心者总结~
这张图就是我折腾后的结果,颜色真的亮瞎眼,在一个就是我用了Xcode5,版本对于我来说并不是最主要的,我主页不搞开发啦。
跟UIbutton UIscrollView等等不同,UItableView分了dataSource和delegate
dataSource是UITableViewDataSource类型,主要为UITableView提供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和reordering),并根据用户的操作进行相应的数据更新操作,如果数据没有更具操作进行正确的更新,可能会导致显示异常,甚至crush。
delegate是UITableViewDelegate类型,主要提供一些可选的方法,用来控制tableView的选择、指定section的头和尾的显示以及协助完成cell的删除和排序等功能。
头文件
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ NSArray *dataList; } @property (nonatomic , retain)NSArray *datalist; @end下面是实现文件
首先是要位UItableView做一些前期的准备,
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *list = [NSArray arrayWithObjects:@"武汉",@"北京",@"深圳", nil];//列表数据 self.datalist = list; UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped]; tableView.dataSource = self; tableView.delegate = self; [self.view addSubview:tableView];//添加到View中 }指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; }
每个分区的行数,这里指定了数组中的键值count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.datalist count]; }开始cell,我从这里粘过来的 http://www.open-open.com/lib/view/open1345219607881.html
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *MyIdentifier = @"MyIdentifier"; //相当于一个行标识 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; //tableViewCell重绘 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier] ; } NSUInteger row = indexPath.row; //获取行号 NSString *titleStr = [datalist objectAtIndex:row];//获取数据 cell.textLabel.text = titleStr;//数据显示 cell.textLabel.textColor = [UIColor whiteColor]; cell.detailTextLabel.text=@"INformation"; cell.imageView.image = [UIImage imageNamed:@"XXX.png"]; cell.detailTextLabel.textColor = [UIColor redColor]; return cell; }
cell.selectedBackgroundView.backgroundColor = [UIColor brownColor];//这里是选中时的颜色,我修改博客方便就在这里直接单写了。剩下的一些配置可以多多探索。这些目前我觉得差不多够用了。
这里有四个UITableViewCellStyle
typedef enum { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle } UITableViewCellStyle;
风格对应
设定行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 90; }隔行换色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row] % 2 == 0) { cell.backgroundColor = [UIColor blueColor]; } else { cell.backgroundColor = [UIColor greenColor]; } }执行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"执行删除操作"); }缩进设置
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { return 2;//return [indexPath row]; }
分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"XXX"; }目前这些已经足够我用了,我歇过来作为主要的一个记录和整理方便给我以后的使用,如果你看到了我写的这篇文章,看到我哪些概念或者错误请告诉我,十分感谢,学习IOS开发一步一步走啊走。