IOS development-UI (8) TableView,-uitableview

Source: Internet
Author: User

IOS development-UI (8) TableView,-uitableview

Knowledge point:

1. Use UITableView

2. UITableView Segmentation

3. UITableViewCell reuse mechanism

 

======================================

Use UITableView

1. Role of UITableView

2. Create UITableView

-(Id) initWithFrame :( CGRect) frame style :( UITableViewStyle) style;

UITableViewStyle:

UITableViewStylePlain list mode

UITableViewStyleGrouped group mode

// Instantiate a table View

// UITableViewStylePlain list mode

// UITableViewStyleGrouped group mode

UITableView * tableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped]; // sets the proxy tableView. delegate = self; tableView. dataSource = self; [self. view addSubview: tableView];

 

 

3. Associate UITableView with data (above)

1) tableView associates data through proxy

4. NSIndexPath

Used to identify the location of the current cell in tableView

This object has two attributes: section and row,

The former identifies the current cell in the section

The latter indicates the row number in this section.

 

5. Introduction to UITableViewCell

1) Creation Method

-(Id) initWithStyle :( UITableViewCellStyle) style

ReuseIdentifier :( NSString *) reuseIdentifier

// When a View Controller is managed by the navigation controller. the first subview added on the view is a subclass of UIScrollView or UIScrollView. The coordinates of this object are automatically shifted to 64 units.

// Disable this optimization Mechanism

// Self. automaticallyAdjustsScrollViewInsets = NO;

UITableViewCellStyle:

UITableViewCellStyleDefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2

UITableViewCellStyleSubtitle

 

UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];

======================================

UITableView Segmentation

1. Set the tableView Style

UITableViewStyleGrouped

2. Set proxy

1) set the number of segments: 1 is returned by default.

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView

======================================

Common UITableView Methods

UITableViewDataSource

UITableViewDelegate

@ Interface RootViewController () <UITableViewDelegate, UITableViewDataSource> # pragma mark-UITableViewDelegate & UITableViewDataSource // number of returned groups (optional)-(NSInteger) values :( UITableView *) tableView {return 2 ;}

 

// Return the number of rows in A group (default: 1 group)

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {

Return 20;

}

// A UITableViewCell object needs to be returned for each row

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {

// Coordinate object in NSIndexPath table View

// Section-> group

// Row-> row

// Create a UITableViewCell object

/*

Parameter 1: cell type

Parameter 2: Reuse ID

*/

UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];

// Set the cell title

Cell. textLabel. text = @ "Hello everyone ";

// Set the image

Cell. imageView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "% 03ld", indexPath. section * 20 + indexPath. row + 1];

Return cell;

}

1) set the Row Height

-(CGFloat) tableView :( UITableView *) tableView

HeightForRowAtIndexPath :( NSIndexPath *) indexPath

// Set the Row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 100 ;}

 

2) set the field header title

-(NSString *) tableView :( UITableView *) tableView

TitleForHeaderInSection :( NSInteger) section

// Return group header title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [NSString stringWithFormat: @ "% ld group Header ", section];}

 

3) set the end title

-(NSString *) tableView :( UITableView *) tableView

TitleForFooterInSection :( NSInteger) section

// Return the end title of the Group-(NSString *) tableView :( UITableView *) tableView titleForFooterInSection :( NSInteger) section {return @ "I am the end Of the group ";}

 

4) delete/Insert a row (two rows are used together)

// Edit the Event Callback method-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = callback) {// Delete // first Delete the data source [self. dataArr removeObjectAtIndex: indexPath. row]; // refresh the UI // reloadData reload the data once // [_ tableView reloadData]; // refresh with animation (delete) [_ tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationRight];} else {// insert // Insert new data into the data source first [self. dataArr insertObject: @ "Xi'an" atIndex: indexPath. row]; // refresh UI // [_ tableView reloadData]; // refresh with animation (insert) [_ tableView insertRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationRight];} // The returned editing type-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {/* Rows // Delete rows // insert * // return rows; return UITableViewCellEditingStyleInsert ;}

 

 

5) customize and delete the text above

-(NSString *) tableView :( UITableView *) tableView titleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *) indexPath

 

// Call tableView

-(Void) insertRowsAtIndexPaths :( NSArray *) indexPaths

WithRowAnimation :( UITableViewRowAnimation) animation;

-(Void) deleteRowsAtIndexPaths :( NSArray *) indexPaths

WithRowAnimation :( UITableViewRowAnimation) animation;

 

6) Enter edit and cancel edit mode

@ Property (nonatomic, getter = isEditing) BOOL editing

7) How to Make the specified row editable

-(BOOL) tableView :( UITableView *) tableView

CanEditRowAtIndexPath :( NSIndexPath *) indexPath

// Whether to allow edit-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {// The first row does not allow edit example/* if (indexPath. row = 0) {return NO;} */return YES ;}

 

8) How to index

-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView

// Return Index-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {NSMutableArray * newArr = [NSMutableArray new]; // Note: The number of indexes should be equal to the number of groups, if the number of indexes is greater than the number of groups, the remaining indexes are invalid for (char I = 'a'; I <= 'Z'; I ++) {[newArr addObject: [NSString stringWithFormat: @ "% c group", I];} return newArr ;}

 

9) how to jump to a specified line

-(Void) scrollToRowAtIndexPath :( NSIndexPath *) indexPath

AtScrollPosition :( UITableViewScrollPosition) scrollPosition

Animated :( BOOL) animated;

 

10) How to move a row

-(Void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *)

SourceIndexPath toIndexPath: (NSIndexPath *) destinationIndexPath {

// Move a row-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {// sourceIndexPath initial number of rows // destinationIndexPath target number of rows // save an id obj = self. dataArr [sourceIndexPath. row]; // Delete [self. dataArr removeObjectAtIndex: sourceIndexPath. row]; // insert to the target position [self. dataArr insertObject: obj atIndex: destinationIndexPath. row]; for (NSString * str in self. dataArr) {NSLog (@ "str = % @", str );}}

 

11) Select the specified row

-(Void) tableView :( UITableView *) tableView

DidSelectRowAtIndexPath :( NSIndexPath *) indexPath;

// Select a row

// The didSelectRowAtIndexPath is correct.

// DidDeselectRowAtIndexPath Error

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "the number of selected rows is % ld", indexPath. row);/* UITableViewScrollPositionTop move a row to the top of the screen; move a row to the middle of the screen; UITableViewScrollPositionBottom move a row to the bottom of the screen */[tableView scrollToRowAtIndexPath: UITableViewScrollPositionMiddle animated: YES];}

 

12) process the events that the accessoryButton is pressed.

-(Void) tableView :( UITableView *) tableView

AccessoryButtonTappedForRowWithIndexPath :( NSIndexPath *) indexPath

 

======================================

UITableViewCell multiplexing mechanism

1. cell reuse Method

-(Id) dequeueReusableCellWithIdentifier :( NSString *) identifier;

2. Reuse Problems

This cell may not exist during the first dequeue, so you need to determine

If the cell is not in the queue, alloc

# Pragma mark-UITableViewDelegate & UITableViewDataSource // return the rows in A group (one group by default)-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 20;} // each row needs to return a UITableViewCell type object-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// each UITableView has a reusable Queue (array). Whenever you need to return a UITableViewCell object, first, repeat the queue to find whether there are objects of the same type. If yes, use UITableViewCell * cell again = [tableView dequeueReusableCellWithIdentifier: @ "cell"]; // if the reuse queue is not found, create a new object if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @ "cell"];} NSLog (@ "the content displayed before modification is % @", cell. textLabel. text); // set the title of the cell to cell. textLabel. text = [NSString stringWithFormat: @ "% ld row", indexPath. row + 1]; NSLog (@ "the content displayed after modification is % @", cell. textLabel. text); return cell ;}

 

Related Article

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.