0

I have an UITableView its loading number ofsongs from the back end (actually those are song's urls). Those are getting in JSON format. Now my problem is when number of songs are increases the table getting stuck. It hard to scroll. So how I can avoid this?

I'm loading views top of a main view. In main view viewDidLoad

  arrSongList=[[NSMutableArray alloc]init];

  arrSongList=[ws GetSongs];`

Then there is a menu to select song option. When I click on that menu cell it loads the songs view. for loading song view

-(void)getAllSongsAndReloadTable :(id)sender//1
{
    [viewDisplayArea addSubview:viewSongs];
    [self flipView : viewDisplayArea: viewDisplayArea :  UIViewAnimationTransitionFlipFromLeft];
    [progressAlert dismissWithClickedButtonIndex:0 animated:YES];
}

ViewSongs view has the song table. It loads in this way

This is for numberOfRowsInSection

else if (tableView.tag==4) 
{
    return [arrSongList count];
}

This is for cellForRowAtIndexPath

else if (tableView.tag==4)//All Songs
{
    cell4 =nil;

    if (cell4 == nil) {

        NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"SongCell" owner:self options:nil];
        cell4=[nib objectAtIndex:0];
    }


    [cell4 setSelectionStyle:UITableViewCellSelectionStyleNone];

    cell4.lblSong.text=[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"SONGTITLE"];
    cell4.lblArtist.text=[[arrSongList objectAtIndex:indexPath.row] valueForKey:@"ARTISTNAME"];
    NSString *imgUrl=[[arrSongList objectAtIndex:indexPath.row]valueForKey:@"SONGIMG"];
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString: imgUrl]];
    UIImage *img = [[UIImage alloc] initWithData:data];
    cell4.imgSong.image=img;


    NSString *imgUrlLarge=[[arrSongList objectAtIndex:indexPath.row]valueForKey:@"SONGIMGLARGE"];
    NSData *dataLarge=[NSData dataWithContentsOfURL:[NSURL URLWithString: imgUrl]];
    UIImage *imgLarge = [[UIImage alloc] initWithData:dataLarge];
    cell4.imgSongLarge=[[UIImageView alloc]init];
    cell4.imgSongLarge.image=imgLarge;



    [cell4.btnTick addTarget:self action:@selector(tickButtonTapped:) forControlEvents:UIControlEventTouchUpInside] ;

    [cell4.btnAddtoMyList addTarget:self action:@selector(topLeft:) forControlEvents:UIControlEventTouchUpInside];
    return cell4;
}
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
iDia
  • 1,397
  • 8
  • 25
  • 44
  • You can see my answer at this post which may help you: http://stackoverflow.com/questions/16213603/load-large-data-from-multiple-tables-paralelly-multithreading/16214574#16214574 – Amit May 27 '13 at 06:45
  • @amit3117 thnk u so much, How can I update the table, is there any method to update that table when the index.ro reaches to the exact number rartherthan reload method? – iDia May 27 '13 at 06:51
  • 1
    How many songs URL has to be displayed? Table view will not stuck like that. can you post your code then people can give you suggestions – Augustine P A May 27 '13 at 06:53
  • even it can be go for 1000 – iDia May 27 '13 at 06:54
  • Ok can you please post your table view datasource delegates – Augustine P A May 27 '13 at 06:54
  • Yes as Augustine says tableView by default follows a pull model so it should not get stuck like that may you are doing something wrong. Please post code for better suggestion. – Amit May 27 '13 at 07:03
  • Are you using "dequeueReusableCells"? – pbibergal May 27 '13 at 07:07
  • 1
    One more suggestion will be dont create the image from data inside CellforrowatIndexpath method. Instead create an array having images and use these images directly inside this method to improve performance. – Amit May 27 '13 at 07:11
  • Your table getting stuck because of downloading the image instantly. You have to do lazy loading mechanism to do that. You just google for lazy loading images for table view. And one more thing, use dequeueReusableCells instead of creating new cells. – Augustine P A May 27 '13 at 07:29
  • thank you,, what is the lazy loading – iDia May 27 '13 at 07:33

2 Answers2

1

You will be having a array of songs urls. And you would have given number of rows in table as arrayOfUrlSongs.count. In stead of that make

Make a global count int count = 10;

if(arrayOfUrlSongs.count < count)
      return arrayOfUrlSongs.count;
else
{
     return count;
}

Keep a reload button in footer view of table. On click of that increment count by 10 and reload table.

I hope this helps you.

Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
0

You should also consider loading the cells in batches. I'd recommend checking out the Sensible TableView framework. The framework will automatically fetch all the songs from your web service and divide them in batches if you wish. Should save you a ton of work.

Matt
  • 2,391
  • 2
  • 17
  • 18