IOS-UITableView reuse mechanism, image refresh,

Source: Internet
Author: User

IOS-UITableView reuse mechanism, image refresh,

How to Use UITabel View

Reference link http://www.bubuko.com/infodetail-974265.html

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier = @"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];    }    return cell;}

Identifier

It can be seen that a cell is bound with an identifier when it is created. This identifier can be understood as the cell id to identify which reuse queue it belongs.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

Let's take a look at this code and retrieve a cell from the reuse queue. Pay attention to the input parameter identifier. If we compare the reuse queue to a room, the identifier is like the room number, indicates that you want to find someone (cell) from the specified room ). In addition, the identifier of the cell is also put into the specified reuse queue when you enter the queue.

As you can imagine, because all cells in the above Code use the same identifier, they will only go in and out in a reusable queue. If you change the code to the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];    }    return cell;}

When a cell is created, different identifiers are bound to each cell. Therefore, each cell is placed in different queues when it is in the queue.First timeAfter the drop-down of 100 cells, each call to dequeueReusableCellWithIdentifier fails to find the cell in the corresponding reuse queue. Therefore, we need to create 100 cells, which increases the consumption.

Register cell
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];

You can use this method to register a cell when creating a tableView. After registration, the function is to retrieve the cell from the reuse list every time. If the cell does not exist, the system will automatically create a cell for us, and bind the identifier. As you can imagine, you do not need to write this code after registration:

if (!cell) {    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}
Solve the Problem of duplicate list confusion
Refer to the article to solve the problem of duplicate lists.
I will introduce the actual solution in development.
I need to reuse cells, but the data is different, and the length of each CELL is also different. However, the data types are the same.
Reuse CELL,
1. Clear all data displayed by the CELL,
2. assign values to the new data of the CELL and re-deploy the cell through code. And calculate the height of the cell.
3. On the UItabeleView control page, record the cell height through the dictionary (keys is the unique ID of the data displayed by me, and value is the height of the cell)
4. In the proxy method of UItableView,

(-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath,) retrieve the height of the cell and assign values.

Solve the Problem of refreshing images in the list

I want to display the default image in CELL. When the image data is downloaded successfully, refresh the page of the current cell to display the latest image data,

// Custom zoom-in. The image is successfully downloaded and the method for calling the notification is displayed.

-(Void) reloadSelectButtonInfo

{

NSArray * cells = self. myTabelView. visibleCells; // obtain the currently displayed cell

For (int I = 0; I <[cells count]; I ++ ){

MyTableViewCell * cell = cells [I];

[Cell userisreferencewith0000item]; // Method for displaying images in 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.