iOS開發-UITableView表格最佳化

來源:互聯網
上載者:User

標籤:

之前的一篇文章大概講述了一下UITableView的使用,UITableView在iOS的地位和ListView在Android中的地位基本上算是不相上下,關於ListView的最佳化網上的也有很多文章。UITableView蘋果公司本身就已經最佳化了其中的功能,不管你有多少資料,每次載入的時候只是載入當前頁面的資料,以免造成不必要的記憶體佔用。一個非常常見的最佳化就是使用Identifier,也就是唯一標示,將頁面中不用的對象放在緩衝池中,如果有新的對象出現從緩衝池中取出。

頁面配置

頁面配置還是跟上篇文章一樣,一個TableView:

 

不過需要額外的工作的時本次暫時的是服裝資訊,通過彈框修改服裝的價格,需要建立一個服裝類,實現UIAlertViewDelegate協議,標頭檔中的聲明:

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>@property (weak, nonatomic) IBOutlet UITableView *tableView;@end

服裝類的定義:

@interface Dress : NSObject@property (strong,nonatomic) NSString *dressName;@property (strong,nonatomic) NSString *dressDetial;@property (strong,nonatomic) NSString  *dressImage;@end
最佳化與實現  

最佳化之前先實現一些必要的功能,以及一些方法的使用,上篇文章只是涉及了其中的一部分,為了更好的理解,可以先看下實現的效果:

定義儲存資料的數組:

 

@interface ViewController (){    NSArray *imageArr;    NSArray *dressArr;    NSMutableArray *dressList;}@end

 

初始化資料:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    imageArr=[[NSArray alloc]initWithObjects:@"dress1.jpeg",@"dress2.jpg",@"dress3.jpg",nil];    dressArr=[[NSArray alloc]initWithObjects:@"七匹狼",@"森馬",@"傑克瓊斯",@"美特斯邦威",@"以純",@"真維斯",@"海南之家",nil];    dressList=[NSMutableArray arrayWithCapacity:30];    for (NSInteger i=0; i<30; i++) {        Dress *dress=[[Dress alloc]init];        NSInteger imageRandom=arc4random_uniform(3);        NSInteger dressRandom=arc4random_uniform(7);            NSInteger price=arc4random_uniform(1000)+100;                dress.dressImage=imageArr[imageRandom];        dress.dressName=dressArr[dressRandom];        dress.dressDetial=[NSString stringWithFormat:@"促銷價:%ld",(long)price];        [dressList addObject:dress];    }}

 設定行數:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [dressList count];}

 設定分組:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}

 設定儲存格內容,通過reuseIdentifier設定重用的儲存格,dequeueReusableCellWithIdentifier取出可以重用的儲存格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *[email protected]"cacheCell";    //產生唯一的標記    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];    if (cell==nil) {        //設定需要顯示詳情的樣式        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:flag];    }    Dress *dress=dressList[indexPath.row];        UIImage *image=[UIImage imageNamed:dress.dressImage];    [cell.imageView setImage:image];    [cell.imageView setFrame:CGRectMake(0, 0, 80, 50)];    [cell.textLabel setText:dress.dressName];    //設定展示小箭頭    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];    //設定Tag//    [cell setTag:indexPath.row];        [cell.detailTextLabel setText:dress.dressDetial];        NSLog(@"擷取更新之後的行:%ld",indexPath.row);    return cell;}

選中行之後的彈框設定:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"服裝價格:" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"修改", nil];    [alterView setAlertViewStyle:UIAlertViewStylePlainTextInput];    //選中的Cell    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];        UITextField *textField=[alterView textFieldAtIndex:0];        //設定修改的服裝資訊    [textField setText:cell.detailTextLabel.text];        [textField setTag:indexPath.row];    [alterView  show];}

 UIAlterView中的點擊事件:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex==1) {                UITextField *textField=[alertView textFieldAtIndex:0];                NSArray *selectedRow=[self.tableView indexPathsForSelectedRows];                NSIndexPath *indexPath=selectedRow[0];                Dress *dress=dressList[indexPath.row];                dress.dressDetial=textField.text;                        NSLog(@"%@---%ld",textField.text,(long)indexPath.row);                [self.tableView reloadRowsAtIndexPaths:selectedRow withRowAnimation:UITableViewRowAnimationRight];        }}

 設定行高:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 50;}

關於UITable的最佳化:

1.最常用的就是不重複產生儲存格,很常見,很實用;

2.使用不透明的視圖可以提高渲染速度,xCode中預設TableCell的背景就是不透明的;

3.如果有必要減少視圖中的條目,本文中設定textLabel,detialTextLabel,imageView,accessoryType;

4.更新條目的時候不要整體更新,更新選中的即可,建議reloadRowsAtIndexPaths,而不是使用reloadData;

 

iOS開發-UITableView表格最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.