简体   繁体   中英

Disable the table view reloading while scrolling

I am working on a chatting application. I don't want to reload the tableview when scrolling.

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

        @try {

            static NSString *cellIdentifier=@"cell";
            MessageCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if(cell==nil)
            {
                cell=[[MessageCell alloc]initMessagingCellWithReuseIdentifier:cellIdentifier];
            }
            if(self.check)
            {
                cell.sent=YES;
                cell.messageLabel.text=[self.sendArray objectAtIndex:indexPath.row];
            }
            else
            {
                cell.sent=NO;
                cell.messageLabel.text=[self.sendArray objectAtIndex:indexPath.row];
            }
            cell.timeLabel.text=@"27-04-2016";
            cell.backgroundColor=[UIColor clearColor];
            return cell;
        }
        @catch (NSException *exception) {
        NSLog(@"Error in func:%s, line:%d with reason:%@",__func__,__LINE__,exception.reason);

        }

}

When I send the message it shows on the right( self.check is YES ), and when I receive the message it shows on the left ( self.check is NO ) but the problem is when I'm scrolling , the tableview reloads itself and shows the entire message (either send or received) on the left because self.check is NO .

How can I stop this happening?

In your MessageCell class, you have to override prepareForReuse() method and reset all flags. When we scroll tableview it reuses the constructed cells. Therefore, it also reuses the flags. Here is the example

override func prepareForReuse() 
{
    super.prepareForReuse();
    currencyCodeLbl.text = nil;
    currencyNameLbl.text = nil;
    isSelf = false;
    flagImage.hnk_cancelSetImage();
    flagImage.image = nil;
}

First you should create a instance property to keep track of the tableView state. Based on the value of this property you can either reload or not reload your table view (wherever in your code you're doing this).

@property (assign, nonatomic) BOOL isTableViewScrolling;

To keep track if someone is dragging/scrolling the tableview you can use the scrollView delegate methods:

scrollViewWillBeginDragging:

scrollViewDidEndDecelerating:

and set isTableViewScrolling accordingly

Self.check should be an array and it should be a specific value for each row. So you could check it like:

if(self.check[indexPath.row]) {
    // Do something!
}

I'm glad this has helped you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM