[ios专项]TableView 全面介绍 资料整理

来源:互联网 发布:免费的统计软件 编辑:程序博客网 时间:2024/05/22 04:31


po : TableView在APP中太常见了,首先要有概念:cell,就是表格试图中的一行


具体先看下table'view的一些基本概念介绍:

http://www.cocoachina.com/applenews/devnews/2013/0723/6661.html  UITableView 简单解析


在UITableView中。最重要的就是data source中的两个方法。

<span style="font-size:14px;">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; </span>

什么是data source。字面意思就是很明白,数据的来源。一般情况下我们会设置拥有UITableView的这个UIViewController为他的data source。因为根据MVC来说。
 
UITableView是View,UIViewController是Controller。View需要的数据,应该是Controller去跟Model协调然后获得,以后由Controller去给View来进行显示。View永远的不去直接跟Model联系。这样当UITableView初始化的时候。他就会去问他的data source。我需要显示多少行数据啊。每一行的数据都是什么内容啊。这时候UIViewController应该已经从Model拿到了数据。然后通过- (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section;告诉UITableview,恩你的这一组要显示n条数据。又用- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath函数告诉UITableView说,第几组第几条数据的具体内容是什么。
 
UITableView还有一个比较犀利的地方就是如果你的数据有10000条。它肯定不是把10000条都加载进来。而是只加载需要显示的条目数据。这样设计,使得UITableView的流畅程度大大提高。值得注意的是,如果Cell里面的数据是从网络 or Core Data等其他地方读取的。我们应该把读取动作写成异步的。不阻塞主线程。取到数据以后在回答主线程去刷新UI。

2. 还有个小哥 也写得很全面,在1的基础上再看2,效果更好

iOS学习之Table View的简单使用  http://blog.csdn.net/totogo2010/article/details/7642908

Table View简单描述:

    在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View主要分为以下两种:






对于UITableView,我們有一些特殊的概念和术语,比如说我们成Table View的一行为Cell,而许多的Cell可以组成Section,每个Section上下又分別有Header和Footer,许多个的Section则组成了整个Table ,当然Table也有Header和Footer,下面看两种图就能明白这几个拗口名词了:



3. 以上知道tableview怎么用之后,如果再想进一步自己定义cell的话,可用看这里:

http://www.cocoachina.com/newbie/tutorial/2012/0612/4353.html自定义TableViewCell
除了基本的@property @synthesize 代码填充之外,

核心代码:


11.3 在viewDidLoad方法中添加代码:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //加载plist文件的数据和图片    NSBundle *bundle = [NSBundle mainBundle];    NSURL *plistURL = [bundle URLForResource:@"friendsInfo" withExtension:@"plist"];        NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];        NSMutableArray *tmpDataArray = [[NSMutableArray alloc] init];    NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];    for (int i=0; i<[dictionary count]; i++) {        NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1];        NSDictionary *tmpDic = [dictionary objectForKey:key];        [tmpDataArray addObject:tmpDic];                NSString *imageUrl = [[NSString alloc] initWithFormat:@"%i.png", i+1];        UIImage *image = [UIImage imageNamed:imageUrl];        [tmpImageArray addObject:image];    }    self.dataList = [tmpDataArray copy];    self.imageList = [tmpImageArray copy];}

11.5 在@end之前添加代码:

<span style="font-size:14px;">#pragma mark -#pragma mark Table Data Source Methods- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.dataList count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";        static BOOL nibsRegistered = NO;    if (!nibsRegistered) {        UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];        [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];        nibsRegistered = YES;    }        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];         NSUInteger row = [indexPath row];    NSDictionary *rowData = [self.dataList objectAtIndex:row];        cell.name = [rowData objectForKey:@"name"];    cell.dec = [rowData objectForKey:@"dec"];    cell.loc = [rowData objectForKey:@"loc"];    cell.image = [imageList objectAtIndex:row];         return cell;}#pragma mark Table Delegate Methods- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return 60.0;}- (NSIndexPath *)tableView:(UITableView *)tableView   willSelectRowAtIndexPath:(NSIndexPath *)indexPath {     return nil;}</span>


近期准备做几个demo APP, tableview应该会用到很多,务必要搞好这玩意。。




0 0
原创粉丝点击