应用程序之UITableView的Plain用法和cell缓存池优化

  • 效果展示
  • 过程分析
  • 代码实现
  • cell缓存池优化

一、效果展示

二、过程分析

  1. 首先通过三步创建数据,展示数据
  2. 监听选中某一个cell时调用的方法
  3. 在cell中创建一个对话框
  4. 修改对话框中的值,并且重新刷新选中的那个cell

三、代码实现

contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;

+ (id)contactWithIcon:(NSString*)icon title:(NSString*)title desc:(NSString*)desc;

@end

contact.m

#import "Contact.h"

@implementation Contact

+ (id)contactWithIcon:(NSString *)icon title:(NSString *)title desc:(NSString *)desc
{
    Contact *contact = [[Contact alloc] init];
    contact.icon = icon;
    contact.title = title;
    contact.desc = desc;
    
    return contact;
}

@end

ViewController.m

//
//  ViewController.m
//  01-UITableView的单组数据
//
//  Created by apple on 14-4-3.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
{
    NSMutableArray *contacts;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    Contact *contact1 = [Contact contactWithIcon:@"010.png" title:@"盖聂" desc:@"剑圣"];
    Contact *contact2 = [Contact contactWithIcon:@"011.png" title:@"白凤" desc:@"轻功"];
    Contact *contact3 = [Contact contactWithIcon:@"012.png" title:@"胜七" desc:@"巨阙"];
    
    contacts = [NSMutableArray array];
    [contacts addObjectsFromArray:@[contact1, contact2, contact3]];

    
    for (int i = 0; i < 20; i++) {
        NSString *icon = [NSString stringWithFormat:@"01%d.png", i%9];
        NSString *title = [NSString stringWithFormat:@"人物%d", i + 1];
        Contact *contact = [Contact contactWithIcon:icon title:title desc:title];
        
        [contacts addObject:contact];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return contacts.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    Contact *contact = contacts[indexPath.row];
    
    cell.textLabel.text = contact.title;
    cell.detailTextLabel.text = contact.desc;
    cell.imageView.image = [UIImage imageNamed:contact.icon];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
//选中某一个cell的时候调用该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Contact *c = contacts[indexPath.row];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:c.title message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert textFieldAtIndex:0].text = c.desc;
    alert.tag = indexPath.row;
    
    [alert show];
}
//监听点击了UIAlertView里面的按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0) return;
    
    Contact *c = contacts[alertView.tag];
    
    c.desc = [alertView textFieldAtIndex:0].text;
    
    //更新全部数据
    //[_tableView reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:alertView.tag inSection:0];
    NSArray *indexs = @[indexPath];
    [_tableView reloadRowsAtIndexPaths:indexs withRowAnimation:UITableViewRowAnimationBottom];
}

@end

 四、cell缓存池优化

  1. 用到时创建(苹果已经帮我们完成)
  2. cell缓存池中取出可循环的cell

So easy

static NSString *cellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell];
    }
    
    NSLog(@"%p--%d", cell, indexPath.row);

通过打印内存地址可以看出,cell被重复利用了

原文地址:https://www.cnblogs.com/letougaozao/p/3642767.html