简体   繁体   中英

How restrict tableview to fill cell second time if it filled first time

In my TableView I am setting image and two label, it filling fine. But when I scrolling TableView down and then re view that filled cell than it will refill. now i am filling my ImageView with the image which come from URL so it will create load for my user.

So can i restrict my TableView that if my cell is filled first time that it will not fill again.

This is my code :

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell * cell = [atableView  dequeueReusableCellWithIdentifier: nil];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];

        ChannelList *channelList = nil;
        if (searchedData.count ==0) {
            channelList = [channelAllData objectAtIndex:[indexPath section]];
        } else {
            channelList = [searchedData objectAtIndex:[indexPath section]];
        }


        UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
        aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
        NSString * mystr = [channelList channelLogo];

        if (mystr.length !=0) {
            //get a dispatch queue
            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            //this will start the image loading in bg
            dispatch_async(concurrentQueue, ^{
                NSString * str = [NSString stringWithFormat:@"http://manektech.net/mttv/Channel/%@",[channelList channelLogo]];
                NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];

                //this will set the image when loading is finished
                dispatch_async(dispatch_get_main_queue(), ^{
                    aImageView.image = [UIImage imageWithData:image];
                });
            });
        } else {
            aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
        }

        [cell addSubview:aImageView];


        UILabel *nameTextLabel =  [[UILabel alloc] initWithFrame: CGRectMake(80, 15, 175, 20)];
        nameTextLabel.textColor = [UIColor blackColor];
        nameTextLabel.backgroundColor = [UIColor clearColor];
        nameTextLabel.text = [channelList channelName];
        nameTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
        [cell addSubview:nameTextLabel];

        UILabel *genreTextLabel =  [[UILabel alloc] initWithFrame: CGRectMake(80, 35, 175, 20)];
        genreTextLabel.textColor = [UIColor grayColor];
        genreTextLabel.backgroundColor = [UIColor clearColor];

        NSString * str = [NSString stringWithFormat:@"%@ | %@",[channelList channelType],[channelList channelCounry]];

        genreTextLabel.text = str;
        genreTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
        [cell addSubview:genreTextLabel];


    cell.selectionStyle = UITableViewCellSelectionStyleGray;


    }
    return cell;

}

Try this logic may be it will wrk for you..

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

     NSString *SimpleTableIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row]; /// set different idenfifier for all cell

    UITableViewCell * cell = [atableView  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleTableIdentifier] autorelease];
        /// Add UIImageView and UILable here 
    }    
}

i hope this helpful to you....

You should use SDWebImage to avoid lazy table scrolling. It uses caching and avoid requesting images URL everytime.

Crete Your UILabel and UIImageview in Between

if(cell == nil) 
{
    cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault

  .
  .
  .

    // create `UILabel` and `UIImageview`  here add add it to cell.contentView
}

And both in cell.contentView

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