iOS Development-UI (8) TableView

Keywords: iOS

Knowledge points:

1.UITableView uses

2.UITableView Segmentation Function

3.UITableViewCell reuse mechanism

 

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

UITableView uses

1.UITableView role

2.UITableView Creation

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

UITableViewStyle:

UITableViewStylePlain List Mode

UITableViewStyleGrouped Grouping Mode

// Instantiate a table view

// UITableViewStylePlain List Mode

// UITableViewStyleGrouped Grouping Mode

    

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    //Setting agent

    tableView.delegate = self;

    tableView.dataSource = self;

    [self.view addSubview:tableView];

 

 

3.UITableView associated data (above)

1) tableView associates data through proxy

   

    4.NSIndexPath

Mainly used to identify the location of the current cell in tableView

The object has two attributes, section and row.

The former identifies the current cell in the number of section s

The latter represents the row in the section

 

5. Introduction to UITableViewCell

1) How to create

       - (id)initWithStyle:(UITableViewCellStyle)style 

  reuseIdentifier:(NSString *)reuseIdentifier

// When a view controller is managed by a navigation controller, if the first sub-view added above self.view is a subclass of UIScrollView or UIScrollView, the coordinates of the object will automatically be offset 64 units downward.

// Turn off this optimization mechanism

    //self.automaticallyAdjustsScrollViewInsets = NO;

UITableViewCellStyle:

UITableViewCellStyleDefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2

UITableViewCellStyleSubtitle

 

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

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

UITableView Segmentation Function

1. Setting the style of tableView

     UITableViewStyleGrouped

2. Setting up agents

Set the number of segments: default return 1

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

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

Common methods of UITableView

UITableViewDataSource 

UITableViewDelegate

@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

 #pragma mark- UITableViewDelegate&UITableViewDataSource

//Return the number of arrays (optional implementation)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return 2;

}

 

// Returns several rows in a group (default is group 1)

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

   

    return 20;

}

// Each row needs to return an object of UITableViewCell type

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

// Coordinate Objects in NSIndexPath Table View

// section - > Group

// row - > row

    

    

// Create objects of UITableViewCell type

    /*

Parametric 1: cell type

Parametric 2: Reuse identification

     */

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

    

// Set the cell's title

cell.textLabel.text = Hello everyone.

// Setting Pictures

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

    

    return cell;

    

}

1) Setting row height

  - (CGFloat)tableView:(UITableView *)tableView 

       heightForRowAtIndexPath:(NSIndexPath *)indexPath

//Set row height

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

 

2) Setting paragraph headers

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

  titleForHeaderInSection:(NSInteger)section

//Return the header title

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"The first%ld Group header",section];
}

 

3) Setting the title at the end of the paragraph

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

  titleForFooterInSection:(NSInteger)section

//Returns the end of the group title

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"I am the tail of the group.";

}

 

4) Delete/insert a line (two together)

//Callback method for editing events

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //delete

        //First delete the data source

        [self.dataArr removeObjectAtIndex:indexPath.row];

        //Refresh UI

        //reloadData Reload the data

        //[_tableView reloadData];

        //With animation refresh (delete)

        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

    }else{

        //insert
        //First insert new data into the data source

        [self.dataArr insertObject:@"Xi'an" atIndex:indexPath.row];

        //Refresh UI

        //[_tableView reloadData];

        //With animation refresh (insert)

        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

//Edit type returned

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    /*

     UITableViewCellEditingStyleDelete //delete

     UITableViewCellEditingStyleInsert  //insert

 

     */

    //return UITableViewCellEditingStyleDelete;

    return UITableViewCellEditingStyleInsert;

}

 

 

5) Customized deletion of the above text

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

 

// tableView call

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths 

      withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths 

              withRowAnimation:(UITableViewRowAnimation)animation;

 

6) Enter editing and cancel editing mode

  @property(nonatomic,getter=isEditing) BOOL editing

 

7) How to make a specified row editable

  - (BOOL)tableView:(UITableView *)tableView 

      canEditRowAtIndexPath:(NSIndexPath *)indexPath

  

 //Whether editing is allowed

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    //The first line does not allow editing examples

    /*

    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 arrays. If the number of indexes is greater than the number of arrays, the remaining indexes will be 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 line

  - (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 row number

    //destinationIndexPath Target row number

  //Keep a copy

    id obj = self.dataArr[sourceIndexPath.row];
    //delete

    [self.dataArr removeObjectAtIndex:sourceIndexPath.row];

    //Insert into 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

// didSelectRowAtIndexPath is correct

// didDeselectRowAtIndexPath error

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"Number of rows selected%ld",indexPath.row);

    /*

     UITableViewScrollPositionTop Move a row to the top of the screen

     UITableViewScrollPositionMiddle Move a line to the middle of the screen

     UITableViewScrollPositionBottom Move a row to the bottom of the screen

     */

    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

 

12) Handling events pressed by accessoryButton

  - (void)tableView:(UITableView *)tableView

accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

 

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

UITableViewCell Reuse Mechanism

   

1.cell reuse

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

2. The problem of reuse

The cell may not exist at the time of the first dequeue, so it needs to be judged.

If there is no cell in the queue, an alloc is required

#pragma mark- UITableViewDelegate&UITableViewDataSource

//Returns several rows in a group (default is group 1)

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

    return 20;

}

//Each row needs to return one UITableViewCell Objects of type

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


    //In every UITableView Each of them has a multiplexing queue (array),Whenever one needs to be returned UITableViewCell When the object of type is reused, it first finds out whether it has the same type of object in the queue, and if so, it takes it out and uses it again.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If no reuse queue is 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 up cell The title is

    cell.textLabel.text = [NSString stringWithFormat:@"%ld That's ok",indexPath.row + 1];
    NSLog(@"The modified display is as follows%@",cell.textLabel.text);
    return cell;

}

Posted by the DtTvB on Sat, 23 Mar 2019 13:39:52 -0700