Data repeat in UITableView when scrolling

前端 未结 4 1897
青春惊慌失措
青春惊慌失措 2020-12-10 22:46

I use bellow codes to show data on TableView but when scrolling, data repeat and other data lost.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tab         


        
4条回答
  •  不知归路
    2020-12-10 23:26

    This problem is generated because Your UITableView Reuse Cell instant of create new one. I give you some suggestion that may be helpful for you.

    1) add Controller in between

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
      {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            /// Your code of initialization of controller;
        }
    
        /// set property of controllers such like (Text UILabel, image of UIImageView...etc) .
     return cell;
    
    }
    

    1) Add Your Controller to cell.contentView.

    EDITED:

    Before You follow my Edited Answer i want to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView, so be careful for it.

    But it is better for if UITableView Have Limited row about (50-100 may be ) os use following code if is it suitable for you.

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
              /// Your whole code of controllers;
        } 
    

提交回复
热议问题