IOS开发学习笔记(四)——使用TableView

这一小节我们演示如何处理一个列表型的数据。


  1. 我们在界面中添加一个TableView;
  2. 紧接着添加一个plist类型的文件,可以通过New File->Resource->Property List方式创建,如果是已有文件,拷贝到Supporting Files目录,这个文件是一个xml文件,我是用的测试文件参照:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>二手手机</key>
    	<string>张三</string>
    	<key>二手电脑</key>
    	<string>李四</string>
    	<key>数码产品</key>
    	<string>王五</string>
    	<key>办公设备</key>
    	<string>赵六</string>
    	<key>二手家电</key>
    	<string>钱七</string>
    	<key>二手家具</key>
    	<string>孙八</string>
    	<key>居家日用</key>
    	<string>周九</string>
    	<key>服饰鞋帽</key>
    	<string>吴十</string>
    	<key>母婴用品</key>
    	<string>张三</string>
    	<key>美容护理</key>
    	<string>李四</string>
    	<key>艺术收藏</key>
    	<string>王五</string>
    	<key>文体乐器</key>
    	<string>赵六</string>
    	<key>食品保健</key>
    	<string>钱七</string>
    	<key>电玩网游</key>
    	<string>孙八</string>
    	<key>图书音像</key>
    	<string>周九</string>
    	<key>工程机械</key>
    	<string>吴十</string>
    </dict>
    </plist>
    

    注意Key不能重复,但Value可以重复。
  3. 接下来我要将文件内容加载成我能够使用的集合形式,所以我首先声明成员变量:
    @interface ViewController : UIViewController
    
    @property (nonatomic, strong) NSDictionary *contents;
    @property (nonatomic, strong) NSArray *keys;
    
    @end
    

  4. 并在页面启动时加载文件,初始化变量:
    @synthesize contents, keys;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
        contents = [[NSDictionary alloc] initWithContentsOfFile: file];
        keys = [contents allKeys];
        
    }

  5. ok,我们现在已经已经有了集合类型的值,并且已经完成了初始化,下面的工作是对TableView内容填充。这里需要又使用到delegate,同时也请将该TableView的dataSource和delegate关联到File Owner
    @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

  6. 需要实现的方法列表如下:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


  7. 编写代码,其中numberOfRowsInSection返回行数:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [keys count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        NSString *title = [keys objectAtIndex:[indexPath row]];
        
        [[cell textLabel] setText: title];
        
        return cell;
    }

    即可实现列表效果。
  8. 注意,正常情况下cell如果过多,每次滑动时都会重新生成一个cell,这样会导致资源的重复申请与释放工作,所以IOS上我们可以如下优化显示:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        NSString *title = [keys objectAtIndex:[indexPath row]];
        
        [[cell textLabel] setText: title];
        
        return cell;
    }


  9. 下面我们看看分组效果的实现,需要实现的接口:
    @property (nonatomic, strong) NSDictionary *contents;
    @property (nonatomic, strong) NSArray *keys;
    @property (nonatomic, strong) NSDictionary *users;
    @property (nonatomic, strong) NSArray *userkeys;
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;


  10. 初始化属性:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
        contents = [[NSDictionary alloc] initWithContentsOfFile: file];
        keys = [contents allKeys];
        
        file = [[NSBundle mainBundle] pathForResource:@"users" ofType:@"plist"];
        users = [[NSDictionary alloc] initWithContentsOfFile: file];
        userkeys = [contents allKeys];
    }


  11. 实现代码:
    // 2 sections
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        if (0 == section) {
                return [contents count];
        } else {
                return [users count];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        NSString *title;
        if (0 == [indexPath section]) {
            title = [keys objectAtIndex: [indexPath row]];
        } else {
            title = [userkeys objectAtIndex: [indexPath row]];
        }
         
        
        [[cell textLabel] setText: title];
        
        return cell;
    }
    
    
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        if (0 == section) {
            return @"第一小节";
        } else {
            return @"第二小节";
        }
    }

    注意你需要设置TableView为"Grouped"(在UI编辑器中设置控件属性)。
  12. 我们再看一个标题、文字的效果,注意要先修改样式,然后分别设置textLabel和detailTextLabel的text属性:
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        }
        
        NSString *title;
        if (0 == [indexPath section]) {
            title = [keys objectAtIndex: [indexPath row]];
        } else {
            title = [userkeys objectAtIndex: [indexPath row]];
        }
         
        NSString *text;
        if (0 == [indexPath section]) {
            text = [contents objectForKey:[keys objectAtIndex: [indexPath row]]];
        } else {
            text = [users objectForKey:[userkeys objectAtIndex: [indexPath row]]];
        }
        
        [[cell textLabel] setText: title];
        [[cell detailTextLabel] setText: text];
        
        return cell;


  13. 最后设置图片,其中前面和后面的图片设置代码不一样:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        }
        
        NSString *title;
        if (0 == [indexPath section]) {
            title = [keys objectAtIndex: [indexPath row]];
        } else {
            title = [userkeys objectAtIndex: [indexPath row]];
        }
         
        NSString *text;
        if (0 == [indexPath section]) {
            text = [contents objectForKey:[keys objectAtIndex: [indexPath row]]];
        } else {
            text = [users objectForKey:[userkeys objectAtIndex: [indexPath row]]];
        }
        
        [[cell textLabel] setText: title];
        [[cell detailTextLabel] setText: text];
        
        // set image in front of text
    	NSString *imagefile = [[NSBundle mainBundle]  pathForResource:@"cellimage" ofType:@"png"];
    	UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagefile];
    	[[cell imageView]setImage:image];
        // set image behind after text
    	cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        
        return cell;
    }
    


  14. 搞定~~~

你可能感兴趣的:(ios,UITableViewCell,UITableView)