Ios-uitableview Primer (2)

Source: Internet
Author: User

1. For TableView, the view of each item is basically the same. The only difference is data.

iOS provides a way to cache views and data. In-UITableViewCell *) TableView:cellforrowatindexpath:

Create a label for the cache    static nsstring *[email protected] "celltable";    First get UITableViewCell    UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id] from the cache;    If not, the code is created.    if (cell==nil) {        Cell=[[uitableviewcell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier : ID];    }

2

Create a system-supplied icon for each item to the right        Cell.accessorytype=uitableviewcellaccessorycheckmark;

3. Modify the display of an item by animation

[TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];

4. Delete an item by animation

[Self.tableview deleteRowsAtIndexPaths:self.indexPaths Withrowanimation:uitableviewrowanimationleft];

CSZViewController.h The overall code is as follows: declares TableView protocol and data source

#import <UIKit/UIKit.h> @interface Cszviewcontroller:uiviewcontroller <uitableviewdatasource, Uitableviewdelegate> @property (Weak, nonatomic) Iboutlet UITableView *tableview;-(ibaction) Trashclick: (ID) sender ; @end

CSZVIEWCONTROLLER.M the TableView code is as follows:

#pragma mark-datasource#pragma mark number of rows per column-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: ( Nsinteger) section{return self.array.count;} #pragma mark creates view-(UITableViewCell *) TableView for each line: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *)    indexpath{//Create a label for the cache static NSString *[email protected] "celltable";    First get UITableViewCell UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id] from the cache;    If not, the code is created.    if (cell==nil) {Cell=[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];    } Cell.textlabel.text=self.array[indexpath.row];        [email protected] "description ..."; if ([Self.deletearr ContainsObject:cell.textLabel.text]) {//Create a system-supplied icon to the right of each item Cell.accessorytype=uitablevi    Ewcellaccessorycheckmark;    }else {cell.accessorytype=uitableviewcellaccessorynone;    } return cell; } #pragma mark-uitableviewdelegate#pragma maRK Click on each item call-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[TableView        Deselectrowatindexpath:indexpath Animated:yes]; if ([Self.deletearr Containsobject:self.array[indexpath.row]]) {[Self.deletearr removeobject:self.array[indexpath.        Row]];    [Self.indexpaths Removeobject:indexpath];        }else {[Self.deletearr addobject:self.array[indexpath.row]];    [Self.indexpaths Addobject:indexpath]; } [TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft];}    #pragma mark returns height per row-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ return 70;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.