Label is stop update when navigate to another viewcontroller - ios

I am uploading file on server and showing percention on label,label is inside in the table cell, but when we navigate to another view ,its stop update, updated percentage is only show when we scroll table..
-(void)sendReplyOnServer:(NSMutableDictionary*)params
withMessage:(Message*)sendMessage
withIndex:(NSUInteger)deliveredIndex {
[self addReplyInDiscussion:sendMessage.disscussionId];
[[APIClient sharedClient] postReply:params andBlock:^(NSDictionary *responseDictionary,
NSError *error) {
#try {
[[DataManager sharedManager].sendMessages removeObject:sendMessage];
if (!error){
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary :[[responseDictionary objectForKey:#"response"] objectForKey:#"data"] ];
// [dict setObject:#"0" forKey:#"message_status"];
NSLog(#"%#",dict);
Message *deliveredMessage =
[Message modelObjectWithDictionary:dict];
if ([[responseDictionary objectForKey:#"response"] objectForKey:#"document"]){
Document *doc = [Document modelObjectWithDictionary:[[responseDictionary objectForKey:#"response"] objectForKey:#"document"]];
if ([DataManager sharedManager].documentListArray.count == 0){
[DataManager loadDocumentList];
}
if (![DataManager sharedManager].allDocumentsList){
[DataManager sharedManager].allDocumentsList = [[NSMutableArray alloc] init];
}
NSString *documentID = #"1";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"documentId == %#",documentID];
NSArray *filteredArray = [[DataManager sharedManager].documentListArray filteredArrayUsingPredicate:predicate];
if (filteredArray.count != 0) {
Document *cameraRoll = [filteredArray objectAtIndex:0];
doc.parent = cameraRoll;
[cameraRoll.documents addObject:doc];
}
// Document *cameraRoll = [[DataManager sharedManager].documentListArray objectAtIndex:1];
// [cameraRoll.documents addObject:doc];
[[DataManager sharedManager].allDocumentsList addObject:doc];
[DataManager serializeDocumentList];
[DataManager serializeallDocumentList];
}
// if ([ deliveredMessage.deliveryStatus isEqualToString:#"1" ]) {
deliveredMessage.deliveryStatus = #"2";
//image save in file
// deliveredMessage.attachment.image = sendMessage.attachment.image;
if(sendMessage.attachment.image) {
deliveredMessage.attachment.image = sendMessage.attachment.image;
NSURL *url = [NSURL URLWithString:deliveredMessage.attachment.fileName];
NSString *name = [url lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:name];
// NSLog(#"my path == %#",path);
NSData * binaryImageData = UIImagePNGRepresentation(sendMessage.attachment.image);
[binaryImageData writeToFile:path atomically:YES];
}
// }
if ([sendMessage.disscussionId isEqualToString:disscussionId]) {
[self updateMessageList:deliveredMessage withIndex:deliveredIndex withSendMessage:sendMessage];
} else if ([disscussionId isEqualToString:#""] ) {
NSLog(#"reached in known");
NSString *string = [NSString stringWithFormat:#"dataIdentifier == %# ",sendMessage.disscussionId];
NSPredicate *predicate = [NSPredicate predicateWithFormat:string];
NSArray *filterArray = [[DataManager sharedManager].chatListArray filteredArrayUsingPredicate:predicate];
if (filterArray.count != 0 ) {
DiscussionData *discussionOject = [filterArray objectAtIndex:0];
NSUInteger indexOfManager = [[DataManager sharedManager].chatListArray indexOfObject:discussionOject];
DiscussionData *managerDiscussionOject = [[DataManager sharedManager].chatListArray objectAtIndex:indexOfManager];
string = [NSString stringWithFormat:#"identifier == \"%#\" ",deliveredMessage.identifier];
predicate = [NSPredicate predicateWithFormat:string];
filterArray = [managerDiscussionOject.replies.data filteredArrayUsingPredicate:predicate];
if ( filterArray.count != 0 ) {
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
NSUInteger idx = 0;
for ( Message *message in managerDiscussionOject.replies.data ) {
if ([message.identifier isEqualToString:deliveredMessage.identifier] && [message.identifier isEqualToString:message.internalBaseClassIdentifier]){
[indexes addIndex:idx];
}
idx ++;
}
[managerDiscussionOject.replies.data removeObjectsAtIndexes:indexes ];
[[NSNotificationCenter defaultCenter]
postNotificationName:UPDATE_REPLY_ARRAY
object:managerDiscussionOject];
}
//[DataManager serializeChatList];
}
//This case will be handle when time is same
}
else {
/* NSString *string = [NSString stringWithFormat:#"dataIdentifier == %# ",sendMessage.disscussionId];
NSPredicate *predicate = [NSPredicate predicateWithFormat:string];
NSArray *filterArray = [[DataManager sharedManager].chatListArray filteredArrayUsingPredicate:predicate];
if (filterArray.count != 0 ) {
DiscussionData *discussionOject = [filterArray objectAtIndex:0];
NSUInteger indexOfManager = [[DataManager sharedManager].chatListArray indexOfObject:discussionOject];
DiscussionData *managerDiscussionOject = [[DataManager sharedManager].chatListArray objectAtIndex:indexOfManager];
[DataManager serializeChatList];
}*/
}
}else {
NSLog(#"chat reply error === %#",error.localizedDescription);
sendMessage.deliveryStatus = #"3";
if ([sendMessage.disscussionId isEqualToString:disscussionId]) {
if ( repliesArray.count != 0 ) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self indexOfReplyId:sendMessage.internalBaseClassIdentifier] inSection:0];
//NSIndexPath *indexPath = [NSIndexPath indexPathForRow inSection:0];
ChatDetailBaseTVC *cell = (ChatDetailBaseTVC *)[self.tableViewChatConv cellForRowAtIndexPath:indexPath];
cell.msgDeliveryStatusImageView.image = [UIImage imageNamed:MESSAGE_RETRY_IMAGE];
cell.status = #"3";
if (sendMessage.attachment) {
[cell.fileNameLabel setTitle:[NSString stringWithFormat:#" %#", cell.messageObj.attachment.title ] forState:UIControlStateNormal];
cell.progressValue = 0.0 ;
sendMessage.attachment.progress = #"0%";
}
[self updateContentInsetForTableView:self.tableViewChatConv animated:NO];
[self addReplyInDiscussion:sendMessage.disscussionId];
}
}
}
}#catch(NSException *e) {}
} showSpinner:YES showProgressView:^(float progress, NSString *messageId) {
[self updateCellProgress:progress :messageId :sendMessage];
}];
}
-(void)updateCellProgress:(float) progress :(NSString *)messageId :(Message*)sendMessage{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self indexOfReplyId:messageId] inSection:0];
ChatDetailAttached *cell = (ChatDetailAttached *)[self.tableViewChatConv cellForRowAtIndexPath:indexPath];
cell.messageObj.attachment.progress =[NSString stringWithFormat:#"%.0f%%",progress];
NSLog(#"indx %ld == message id %# progress == %#", (long)indexPath.row, messageId,cell.messageObj.attachment.progress);
if ([cell isKindOfClass:[ChatDetailAttached class]] &&
[sendMessage.internalBaseClassIdentifier isEqualToString:cell.messageObj.internalBaseClassIdentifier]) {
[cell.fileNameLabel setTitle:[NSString stringWithFormat:#" %# %#",cell.messageObj.attachment.progress, cell.messageObj.attachment.title ] forState:UIControlStateNormal];
}
}

put a NSTimer in viewDidLoad and set an action method on the selector of NSTimer:
And reload the tabel view in the action methode.g: [_mytableview reload];

Try to update the cell in main thread. And use block variable when you are using block.
Have a variable to hold the progress, CGFloat _progress, NSIndexPath *uploadIndex,
When upload is clicked save the index of cell to uploadIndex.
Update the progress variable
Reload the cell in showProgressView: block
showProgressView:^(float progress, NSString *messageId) {
dispatch_async(dispatch_get_main_queue(), ^{
_progress = progress; [self.tableViewChatConv reloadRowsAtIndexPaths:#[uploadIndex] withRowAnimation:UITableViewRowAnimationNone];
}
}
In cellForRowAtIndex , if([indexPath isEqual:uploadIndex]) then update the progress into cell, cell.progressValue = _progress ;

Related

UICollectionViewCell one of the section from UICollectionView Cell Repeating same cell for multiple times

I have an old project done by a developer some times ago.. This particular project has 3 collection views such as collectionAnnouncments, collectionNews and collectionBulletin.
The first collectionview collectionAnnouncments is loading a same cell 10 times. But other two sections loading the cell correctly one time as per the response count.
I don't have any idea up to now for this problem. I tried some solutions from the google but I couldn't sorted because of the bad UI and Code implementation by that developer.
Ex- He used one UICollectionView Inside a tableview and using UICollectionView Class for all 3 collectionview and used a general cell for all collectionviews.
declaration... in m file
__weak IBOutlet UITableView *table;
UICollectionView *collectionAnnouncments, *collectionBulletin,
*collectionNews;
Please check the following codes and provide me a better simple solution to fix this issue without any major modifications or re-implementation because I don't have time for that.
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
home.title=[Utilities getLocalizedStringForKey:#"Home"];
[Utilities setNavigationController:self];
//self.label.text=NSLocalizedFailureReasonErrorKey
self.navigationItem.leftBarButtonItem = nil;
__block BOOL newsDone = NO, bulletInDone = NO, announcmentDone = NO, refreshed = NO;
collectionViewDic = [[NSMutableDictionary alloc]init];
[Utilities serverRequest:#{#"getNewsfeed":#"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) {
collectionViewDic[#"news"] = response[#"response"];
NSArray *responseValues = [response[#"response"] allValues]; // An NSArray of NSArrays
NSMutableArray *dictionarys = [NSMutableArray new];
for (NSArray *dictArrays in responseValues) {
for (NSDictionary *dict in dictArrays) {
[dictionarys addObject:dict];
}
}
_newsArray = dictionarys;
NSLog(#"news%#",dictionarys);
// sorting to newest
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"nf_id" ascending:NO];
_sortednewsArray = [_newsArray sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"Sorted news Response -%#", _sortednewsArray);
//Registeriing the collectionview custom cell
// [UICollectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"customCell"];
// [collectionAnnouncments registerClass:[CustomCell class] forCellWithReuseIdentifier:#"customCell"];
// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
newsDone = YES;
if (newsDone && bulletInDone && announcmentDone && !refreshed) {
refreshed = YES;
[table reloadData];
}
} errorBlock:nil];
[Utilities serverRequest:#{#"getBulletin":#"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) {
// NSString *bulletinID = #"wb_id";
collectionViewDic[#"bulletin"] = response[#"response"];
NSArray *responseValues = [response[#"response"] allValues]; // An NSArray of NSArrays
NSMutableArray *dictionarys = [NSMutableArray new];
for (NSArray *dictArrays in responseValues) {
for (NSDictionary *dict in dictArrays) {
[dictionarys addObject:dict];
}
}
_bulletinArray = dictionarys;
NSLog(#"bulletin%#",dictionarys);
// sorting to newest
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"wb_id" ascending:NO];
_sortedbulletinArray = [_bulletinArray sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"Sorted bulletin Response -%#", _sortedbulletinArray);
bulletInDone = YES;
if (newsDone && bulletInDone && announcmentDone && !refreshed) {
refreshed = YES;
[table reloadData];
}
} errorBlock:nil];
[Utilities serverRequest:#{#"getAnnouncement":#"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) {
collectionViewDic[#"announcement"] = response[#"response"];
NSArray *responseValues = [response[#"response"] allValues]; // An NSArray of NSArrays
NSMutableArray *dictionarys = [NSMutableArray new];
for (NSArray *dictArrays in responseValues) {
for (NSDictionary *dict in dictArrays) {
[dictionarys addObject:dict];
}
}
_annArray = dictionarys;
NSLog(#"AnnouncementResponse%#",dictionarys);
NSLog(#" Ann ID%#", [dictionarys valueForKey:#"anc_id"]);
// sorting to newest
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"date_added" ascending:NO];
_sortedAnnArray = [_annArray sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"Sorted Array Response -%#", _sortedAnnArray);
NSLog(#"Sorted Ann Array-count%zd",_sortedAnnArray.count);
NSLog(#"Sorted Ann ID%#", [_sortedAnnArray valueForKey:#"anc_id"]);
announcmentDone = YES;
if (newsDone && bulletInDone && announcmentDone && !refreshed) {
refreshed = YES;
[table reloadData];
}
} errorBlock:nil];
menuView = [Utilities addMenuView:self];
}
-(void)getNewsFeed
{
collectionViewDic = [[NSMutableDictionary alloc]init];
[Utilities serverRequest:#{#"getNewsfeed":#"a6dba37437ced2c3b07469fd6c0661f3"} completionBlock:^(id response) {
if(response != nil)
{
if ([response isKindOfClass:[NSString class]]) {
// print response .
NSLog(#"MAIN RESPONSE!%#",response);
}
//on successfully response, which provided dictionary,
else if ([[response objectForKey:#"success"] boolValue] == true) {
// on true print response data.
collectionViewDic[#"news"] = response[#"response"];
NSArray *responseValues = [response[#"response"] allValues]; // An NSArray of NSArrays
NSMutableArray *dictionarys = [NSMutableArray new];
for (NSArray *dictArrays in responseValues) {
for (NSDictionary *dict in dictArrays) {
[dictionarys addObject:dict];
}
}
_newsArray = dictionarys;
NSLog(#"news%#",dictionarys);
// sorting to newest
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"nf_id" ascending:NO];
_sortednewsArray = [_newsArray sortedArrayUsingDescriptors:#[sortDescriptor]];
NSLog(#"Sorted news Response -%#", _sortednewsArray);
}
//on success response, with failure message which is false.
else if ([[response objectForKey:#"success"] boolValue] == false) {
// handle error on success is false.
}
}
} errorBlock:nil];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#try {
NSString *key = #"";
if ([collectionView isEqual:collectionNews])
key = #"news";
else if ([collectionView isEqual:collectionAnnouncments])
key = #"announcement";
else if ([collectionView isEqual:collectionBulletin])
key = #"bulletin";
NSDictionary *mainDic = collectionViewDic[key];
NSArray *array = mainDic[ [mainDic allKeys][0] ];
return [array count];
} #catch (NSException *exception) {
return 0;
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#try {
//NSString *key = #"";
if ([collectionView isEqual:collectionNews])
return _sortednewsArray.count;
else if ([collectionView isEqual:collectionAnnouncments])
return _sortedAnnArray.count;
else if ([collectionView isEqual:collectionBulletin])
return _sortedbulletinArray.count;
}
#catch (NSException *exception) {
return 0;
}
// return _sortedAnnArray.count + _sortednewsArray.count + _sortedbulletinArray.count;
// return [[collectionViewDic allKeys] count];
// return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"generalCell" forIndexPath:indexPath];
// CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"customCell" forIndexPath:indexPath];
// cell.tag = indexPath.row;
[cell setTag:indexPath.row];
//UILabel *test = [cell.contentView viewWithTag:1];
UIImageView *image = [cell.contentView viewWithTag:1];
//UILabel *cellTitle = [cell.contentView viewWithTag:2];
//UILabel *cellHead =[cell.contentView viewWithTag:2];
if ([collectionView isEqual:collectionAnnouncments]) {
// NSDictionary *mainDic = collectionViewDic[#"announcement"];
// NSArray *array = mainDic[ [mainDic allKeys][indexPath.section] ];
NSDictionary *dic = _sortedAnnArray[indexPath.section] ;
collectionAnnouncments.delegate = self;
collectionAnnouncments.dataSource = self;
// cellHead.text =#"Announcements";
//cellTitle.text = dic[#"anc_title"];
if ([dic[#"announcement_images"] count] > 0)
[image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",ANNOUNCMENT_IMAGES_URL,dic[#"announcement_images"][0][#"wi_image"] ] ]] ;
}
else if([collectionView isEqual:collectionNews]) {
//cellHead.text =#"News";
// NSDictionary *mainDic = collectionViewDic[#"news"];
// NSArray *array = mainDic[ [mainDic allKeys][0] ];
NSDictionary *dic = _sortednewsArray[indexPath.section] ;
collectionNews.delegate = self;
collectionNews.dataSource = self;
//cellTitle.text = dic[#"nf_title"];
if ([dic[#"newsfeed_images"] count] > 0)
[image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",NEWS_IMAGES_URL,dic[#"newsfeed_images"][0][#"wi_image"] ] ]] ;
}
else if([collectionView isEqual:collectionBulletin]) {
// NSDictionary *mainDic = collectionViewDic[#"bulletin"];
// NSArray *array = mainDic[ _sortedbulletinArray[indexPath.section] ];
NSDictionary *dic = _sortedbulletinArray[indexPath.section] ;
collectionBulletin.delegate = self;
collectionBulletin.dataSource = self;
// cellTitle.text = dic[#"wb_title"];
if ([dic[#"bulletin_images"] count] > 0)
if ([dic[#"bulletin_images"][0][#"wi_type"] isEqualToString:#"video"]) {
[image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",BULLETIN_IMAGES_URL,dic[#"ws_thumb"] ] ]] ;
}
else{
[image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",BULLETIN_IMAGES_URL,dic[#"bulletin_images"][0][#"wi_image"] ] ]] ;
}
}
return cell;
}
#pragma mark UITableView Delegate, DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 195.0f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[collectionViewDic allKeys] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"tableCell"];
cell.tag = indexPath.row;
UICollectionView *c = [cell.contentView viewWithTag:111];
//c.scrollEnabled = NO;
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
flow.minimumLineSpacing = flow.minimumInteritemSpacing = 0.0f;
flow.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/1, c.bounds.size.height);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// c.collectionViewLayout = flow;
UIView *header = [cell.contentView viewWithTag:1];
UIImageView *headerImage = [header viewWithTag:2];
UILabel *headerTitle = [header viewWithTag:3];
//NSLog(cell.tag);
switch (indexPath.row) {
case 0:
//NSLog(#"text");
headerImage.image = [UIImage imageNamed:#"Announcemets"];
//headerTitle.text = #"Announcments";
headerTitle.text=[Utilities getLocalizedStringForKey:#"Announcement"];
// NSLog(headerTitle.text);
collectionAnnouncments = c;
[collectionAnnouncments setCollectionViewLayout:flow];
collectionAnnouncments.delegate = self;
collectionAnnouncments.dataSource = self;
[collectionAnnouncments reloadData];
// NSLog(#"text");
break;
case 1:
headerImage.image = [UIImage imageNamed:#"News"];
headerTitle.text=[Utilities getLocalizedStringForKey:#"Business Highlight"];
collectionNews = c;
[collectionNews setCollectionViewLayout:flow];
collectionNews.delegate = self;
collectionNews.dataSource = self;
[collectionNews reloadData];
//NSLog(#"text");
break;
case 2:
headerImage.image = [UIImage imageNamed:#"Bulletin"];
headerTitle.text=[Utilities getLocalizedStringForKey:#"Bulletin Board"];
collectionBulletin = c;
[collectionBulletin setCollectionViewLayout:flow];
collectionBulletin.delegate = self;
collectionBulletin.dataSource = self;
[collectionBulletin reloadData];
//NSLog(#"text");
break;
default:
break;
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[NewsListing class]]) {
NewsListing *c = segue.destinationViewController;
NSInteger row = ((UIButton *)sender).superview.superview.superview.tag;
if (row == 0) {
c.isAnnouncement = YES;
c.listingArray = collectionViewDic[#"announcement"];
}
else if (row == 1) {
c.isNews = YES;
c.listingArray = collectionViewDic[#"news"];
}
else if (row == 2) {
c.isBulletin = YES;
c.listingArray = collectionViewDic[#"bulletin"];
}
}
if ([segue.destinationViewController isKindOfClass:[NewsDetail class]]) {
NewsDetail *c = segue.destinationViewController;
UICollectionViewCell *cell = sender;
NSInteger row = ((UIButton *)sender).superview.superview.superview.tag;
if (row == 0) {
c.isAnnouncement = YES;
// NSDictionary *months = collectionViewDic[#"announcement"];
NSIndexPath *indexPath = [collectionAnnouncments indexPathForCell:cell];
// NSArray *month = months[[months allKeys][0] ];
NSArray *month = _sortedAnnArray;
c.detailDic = month[indexPath.section ];
}
else if (row == 1) {
c.isNews = YES;
// NSDictionary *months = collectionViewDic[#"news"];
NSIndexPath *indexPath = [collectionNews indexPathForCell:cell];
// NSArray *month = months[[months allKeys][0] ];
NSArray *month = _sortednewsArray;
c.detailDic = month[indexPath.section ];
}
else if (row == 2) {
c.isBulletin = YES;
// NSDictionary *months = collectionViewDic[#"bulletin"];
NSIndexPath *indexPath = [collectionBulletin indexPathForCell:cell];
// NSArray *month = months[[months allKeys][0] ];
NSArray *month = _sortedbulletinArray;
c.detailDic = month[indexPath.section ];
}
}
}
#end

call NSString whit multiple parameters

I am a beginner and maybe it is a trivial question.
I have this method:
-(NSString *)getInfoFormediaItem:(MPMediaItemCollection *)list {
NSString *trackCount;
if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"%lu Songs", #""), (unsigned long)[list count]];
} else if([list count] == 1) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"1 Song", #"")];
} else {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"0 Song", #"") ];
}
return [NSString stringWithFormat:#"%#", trackCount];
}
I would like to call it here with a MPMediaItemCollection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playl = [playlistsQuery collections];
MPMediaItem *rowItem = [playl objectAtIndex:indexPath.row];
MPMediaItemCollection * collection = [[MPMediaItemCollection alloc] initWithItems:[NSArray arrayWithObject:rowItem]];
cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
}
I would like to get the number of tracks in each playlist.
It doesn't work. How do I fix? Thanks in advance!
Why are you using performSelector:withObject:? Just call the method directly:
cell.detailTextLabel.text = [self getInfoFormediaItem:collection];
Why are you passing nil to the withObject: parameter? That's why your code goes to the else. list is nil so [list count] will always be 0. You need to pass an actual instance of a MPMediaItemCollection.
Why are you needlessly using stringWithFormat: for the 1 and 0 count checks? Just do:
-(NSString *)getInfoFormediaItem:(MPMediaItemCollection *)list {
NSString *trackCount;
if ([list count] > 1) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"%lu Songs", #""), (unsigned long)[list count]];
} else if([list count] == 1) {
trackCount = NSLocalizedString(#"1 Song", #"");
} else {
trackCount = NSLocalizedString(#"0 Song", #"");
}
return trackCount;
}
Based on your updated question, your cellForRowAtIndexPath code isn't correct for the getting the media collection. The collectionsmethod returns an array of MPMediaCollection objects, not MPMediaItem objects. You need:
MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playl = [playlistsQuery collections];
MPMediaItemCollection *collection = playl[indexPath.row];
Now you can use collection when you call getInfoFormediaItem:.
You simply don't need to call this statement:
cell.detailTextLabel.text = [self performSelector:#selector(getInfoFormediaItem:) withObject:nil];
in your "getInfoFormediaItem"method. You do that in your "cellforrowataIndexPath" method when you define the cell,just call like this:
cell.detailTextLabel.text = [self getInfoFormediaItem:This_Is_A_List_You_Wanna_Pass_To_The_Method];
and you should be good to go.
In addition to the problems pointed out by other posters, your if statement will not work as you want it to:
if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"%lu Songs",
#""), (unsigned long)[list count]];
} else if([list count] == 1) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"1 Song", #"")];
} else {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"0 Song", #"") ];
}
The "if ... >0" clause will match first, before checking for a value of 1, since 1 is > 0. Thus the "if ... == 1" will never evaluate as true. You need to reorder that if statement:
if ([list count] == 1) {
trackCount = NSLocalizedString(#"1 Song", #"");
else if ([list count] > 0) {
trackCount = [NSString stringWithFormat:NSLocalizedString(#"%lu Songs",
#""), (unsigned long)[list count]];
}
else
{
trackCount = NSLocalizedString(#"0 Songs", #"");
}

not able to search the data?

I have added search bar for collection view using programatically .But when i enter the names to search nothing shows.I have some section also and each section contains some data.
Here is my code:
#interface ViewController ()
{
NSMutableArray *arrayPDFName;
NSMutableArray *titleArray;
}
#property (nonatomic,strong) NSArray *dataSourceForSearchResult;
#property (nonatomic) BOOL searchBarActive;
#property (nonatomic) float searchBarBoundsY;
#property (nonatomic,strong) UISearchBar *searchBar;
#property (nonatomic,strong) UIRefreshControl *refreshControl;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSourceForSearchResult = [NSArray new];
titleArray = [NSMutableArray array];
[self getdata];
self.mycollectionView.dataSource = self;
self.mycollectionView.delegate = self;
[self.mycollectionView reloadData];
}
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:#"%02x",result[i]];
}
return ret;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self prepareUI];
}
-(void)getdata {
NSString *userName = #“username#yahoo.com";
NSString *password = #“password”;
NSData *plainData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
base64String=[self sha256HashFor: base64String];
NSString *urlString = #"https://api.exampleport/user/orderid/files";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"GET"];
// basic authendication
NSString *authStr = [NSString stringWithFormat:#"%#:%#", userName, base64String];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
//header-field
NSString *authValue = [NSString stringWithFormat:#"Basic %#", [authData base64Encoding]];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSError * error;
self->arrayPDFName = [[NSMutableArray alloc]init];
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dictOriginal = jsonResults[#“dark”];
[titleArray addObject:[NSString stringWithFormat:#"dark(%#)”, dictOriginal[#"count"]]];
NSDictionary *dictOriginal2 = jsonResults[#"orange”];
[titleArray addObject:[NSString stringWithFormat:#"orange(%#)”, dictOriginal2[#"count"]]];
NSDictionary *dictOriginal3 = jsonResults[#"pencill”];
[titleArray addObject:[NSString stringWithFormat:#"pencill(%#)”, dictOriginal3[#"count"]]];
NSDictionary *dictOriginal4 = jsonResults[#"smart”];
[titleArray addObject:[NSString stringWithFormat:#"smart(%#)”, dictOriginal4[#"count"]]];
NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];
NSLog(#"str: %#", titleArray);
for (NSDictionary *dict in arrayFiles) {
NSMutableArray *arr = [NSMutableArray array];
NSArray *a = dict[#"files"];
for(int i=0; i < a.count; i ++) {
NSString *strName = [NSString stringWithFormat:#"%#",[[dict[#"files"] objectAtIndex:i] valueForKey:#"name"]];
// NSLog(#"str: %#", strName);
[arr addObject:strName];
}
[arrayPDFName addObject:arr];
}
//Get plist path
NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory1 stringByAppendingPathComponent:#"SampleData.plist"];
//NSLog(#"plistPath : %#",plistPath);
//Save data from json to plist
NSString *error1;
returnData = [NSPropertyListSerialization dataFromPropertyList:jsonResults
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(returnData ) {
if ([returnData writeToFile:plistPath atomically:YES]) {
NSLog(#"Data successfully saved.");
}else {
NSLog(#"Did not managed to save NSData.");
}
}
else {
NSLog(#"%#",errorDesc);
}
// NSArray *stringsArray = [NSArray arrayWithContentsOfFile:plistPath];
NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// NSLog(#"str: %#", stringsDictionary);
}
//delegate method for header
-(UICollectionReusableView*) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
HeaderView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"myHeader" forIndexPath:indexPath];
header.myHeaderLabel.text = titleArray[indexPath.section];
return header;
}
#pragma mark - UICollectionView Datasource
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return titleArray.count;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.searchBarActive) {
return self.dataSourceForSearchResult.count;
}
NSArray *a = arrayPDFName[section];
if (a.count == 0)
{
return 1;
}
else
{
return a.count;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(0, 80);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath: (NSIndexPath *)indexPath {
VenueCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"mycell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.imgV.image = [UIImage imageNamed:#"doc1.png"];
if (self.searchBarActive) {
cell.myLabel.text = _dataSourceForSearchResult[indexPath.section][indexPath.row];
}else{
NSArray *a = arrayPDFName[indexPath.section];
if (a.count == 0) {
cell.myLabel.text = #"NO DATA";
return cell;
}
cell.myLabel.text = a[indexPath.row];
}
return cell;
}
//////////////////////////for search bar //////////
#pragma mark - actions
-(void)refreashControlAction{
[self cancelSearching];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// stop refreshing after 2 seconds
[self.mycollectionView reloadData];
[self.refreshControl endRefreshing];
});
}
#pragma mark - search
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"self contains[c] %#", searchText];
self.dataSourceForSearchResult = [self->arrayPDFName filteredArrayUsingPredicate:resultPredicate];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
// user did type something, check our datasource for text that looks the same
if (searchText.length>0) {
// search and reload data source
self.searchBarActive = YES;
[self filterContentForSearchText:searchText
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
[self.mycollectionView reloadData];
}else{
// if text lenght == 0
// we will consider the searchbar is not active
self.searchBarActive = NO;
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self cancelSearching];
[self.mycollectionView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
self.searchBarActive = YES;
[self.view endEditing:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
// we used here to set self.searchBarActive = YES
// but we'll not do that any more... it made problems
// it's better to set self.searchBarActive = YES when user typed something
[self.searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// this method is being called when search btn in the keyboard tapped
// we set searchBarActive = NO
// but no need to reloadCollectionView
self.searchBarActive = NO;
[self.searchBar setShowsCancelButton:NO animated:YES];
}
-(void)cancelSearching{
self.searchBarActive = NO;
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
#pragma mark - prepareVC
-(void)prepareUI{
[self addSearchBar];
[self addRefreshControl];
}
-(void)addSearchBar{
if (!self.searchBar) {
self.searchBarBoundsY = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,self.searchBarBoundsY, [UIScreen mainScreen].bounds.size.width, 50)];
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.tintColor = [UIColor whiteColor];
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Search here";
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
// add KVO observer.. so we will be informed when user scroll colllectionView
[self addObservers];
}
if (![self.searchBar isDescendantOfView:self.view]) {
[self.view addSubview:self.searchBar];
}
}
-(void)addRefreshControl{
if (!self.refreshControl) {
self.refreshControl = [UIRefreshControl new];
self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:#selector(refreashControlAction)
forControlEvents:UIControlEventValueChanged];
}
if (![self.refreshControl isDescendantOfView:self.mycollectionView]) {
[self.mycollectionView addSubview:self.refreshControl];
}
}
-(void)startRefreshControl{
if (!self.refreshControl.refreshing) {
[self.refreshControl beginRefreshing];
}
}
Where i am doing mistake and what i need to change to work.Thanks in advance!
It seems your arrayPDFName have array object and each array object have string object. There are two ways to filter you array.
Way - 1
If you want to get the list of array which has the search string the use SUBQUERY with your predicate. Like below.
NSArray *a1 = #[#"a1", #"a2", #"a3"];
NSArray *a2 = #[#"a1", #"a2", #"a3"];
NSArray *a3 = #[#"a1", #"a2", #"a3"];
NSArray *a4 = #[#"a2", #"a3"];
NSArray *allA = #[a1, a2, a3, a4];
NSPredicate *pre = [NSPredicate predicateWithFormat:#"SUBQUERY(SELF, $a, $a == %#).#count > 0", #"a1"];
NSArray *a = [allA filteredArrayUsingPredicate:pre];
Way - 2
If you want to get the search string alone from each array object then follow the below,
NSMutableArray *fa = [[NSMutableArray alloc] init];
for (NSArray *a in allA) {
NSPredicate *p = [NSPredicate predicateWithFormat:#"self CONTAINS[c] %#", #"a3"];
[fa addObjectsFromArray:[a filteredArrayUsingPredicate:p]];
}
Note: Above code done with same sample data. Replace it as you want.
As per your code replace the below code
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSMutableArray *fa = [[NSMutableArray alloc] init];
for (NSArray *a in arrayPDFName) {
NSPredicate *p = [NSPredicate predicateWithFormat:#"self CONTAINS[c] %#", searchText];
[fa addObjectsFromArray:[a filteredArrayUsingPredicate:p]];
}
self.dataSourceForSearchResult = [NSArray arrayWithArray:fa];
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if(self.searchBarActive)
return 1;
return titleArray.count;
}
In cellForItemAtIndexPath
cell.myLabel.text = _dataSourceForSearchResult[indexPath.row];

How to expand and collapse rows at particular level in UITableView?

Rows of Level 1 should expand and collapse. Suppose if Row 0 is open - that means its status is 1 - and next row's status is 0, if I click on next row whose status is 0, then the row whose status is 1 should get closed.
How can I do that?
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
dictBtnStatus = [[NSMutableDictionary alloc] init];
arraylist = [[NSMutableArray alloc] init];
array5 = [[NSMutableArray alloc] init];
array1 = [[NSMutableArray alloc] init];
objects = #[#"Medical Services In Chicago", #"Medical Services On Campus", #"ABC", #"California"];
// objects = #[#"Medical Services In Chicago", #"Medical Services On Campus"];
arrObjectValueChicago = #[#"Ronald McDonald® Children's Hospital of Loyola", #"Burn Centers", #"Gottlieb Hospitals"];
arrObjectValueCampus = #[#"Cardinal Bernardin Cancer Center1", #"Center for Heart & Vascular Medicine2", #"ABC"];
for (int i = 0; i < [arrObjectValueCampus count]; i++) {
dictListCampus1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"2", #"level",[arrObjectValueCampus objectAtIndex:i], #"name", nil];
[array5 addObject:dictListCampus1];
}
NSDictionary *dictListCampus = [[NSDictionary alloc] initWithObjectsAndKeys:#"Wellness Centers", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
array3 = [[NSMutableArray alloc] init ];
for (int i = 0; i < [arrObjectValueChicago count]; i++){
dictList3 = [[NSDictionary alloc]initWithObjectsAndKeys:#"2", #"level",[arrObjectValueChicago objectAtIndex:i], #"name", nil];
[array3 addObject:dictList3];
}
NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Hospitals", #"name", #"1", #"level", array3, #"Objects", nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
for (int i = 0; i < [objects count]; i++) {
if (i == 0) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 1) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 2) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
}
}
dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, #"Objects", nil];
arrayOriginal = [dictList valueForKey:#"Objects"];
[arraylist addObjectsFromArray:arrayOriginal];
}
#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arraylist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// static NSString *CellIdentifier = #"CustomCellIdentifier";
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (IndentLevel == 0) {
CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellHeader" owner:self options:nil];
cell = self.headercell;
self.headercell = nil;
}
cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
NSLog(#"indexFor level 0 ::%# %d",cell.lblHeader.text,indexPath.row);
return cell;
} else if (IndentLevel == 1) {
CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!cell) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellSubHeader" owner:self options:nil];
cell = self.subheadercell;
self.subheadercell = nil;
NSLog(#"dicbtn %#", dictBtnStatus);
NSString *strName = [[arraylist objectAtIndex:indexPath.row]valueForKey:#"name"];
NSString *str = [dictBtnStatus objectForKey:strName];
NSLog(#"indexFor level 1 ::%# %d", strName, indexPath.row);
if ([str isEqualToString:#"1"]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
}
cell.lblSubHeader.text = strName;
cell.imgShadow.hidden = YES;
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
}
return cell;
} else if (IndentLevel == 2) {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.cells;
self.cells = nil;
}
cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell.btnCall addTarget:self action:#selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tblist deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
CustomCellSubHeader *cell = ((CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath]);
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
}
NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
if ([d valueForKey:#"Objects"]) {
NSArray *ar = [d valueForKey:#"Objects"];
BOOL isAlreadyInserted = NO;
for (NSDictionary *dInner in ar) {
NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
isAlreadyInserted = (index > 0 && index != NSIntegerMax);
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
} else if (IndentLevel == 0) {
NSString *strStatus = [dictBtnStatus objectForKey:strName];
if ([strStatus isEqualToString:#"1"]) {
// NSDictionary *dict = [arraylist objectAtIndex:previousRow];
// if ([dict valueForKey:#"Objects"]) {
NSArray * array = [[arraylist objectAtIndex:previousRow]valueForKey:#"Objects"];
[self miniMizeThisRows:array];
}
}
if (isAlreadyInserted) break;
}
if (isAlreadyInserted) {
if ([arraylist count] - 1 && IndentLevel == 1) {
cell.imgShadow.hidden = NO;
}
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"0" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
} else if (IndentLevel == 0) {
[dictBtnStatus setValue:#"0" forKey:strName];
}
[self miniMizeThisRows:ar];
} else {
NSUInteger count=indexPath.row + 1;
NSMutableArray *arCells = [NSMutableArray array];
for (NSDictionary *dInner in ar) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
previousRow = [indexPath row];
if (IndentLevel == 1) {
[dictBtnStatus setValue:#"1" forKey:strName];
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
} else if (IndentLevel == 0) {
[dictBtnStatus setValue:#"1" forKey:strName];
}
[arraylist insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (indentLevel == 0) {
return 40;
} else if (indentLevel == 1) {
return 25;
} else if (indentLevel == 2) {
CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"]);
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Arial" size:11.5];
CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
if (myStringSize.height > 80) {
myStringSize.height = 50;
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
cell.txtAddress.scrollEnabled = YES;
} else {
cell.txtAddress.scrollEnabled = YES;
myStringSize.height = 40;
}
return myStringSize.height+ 50;
} else return 25;
}
#pragma mark - TableAnimation
- (void)miniMizeThisRows:(NSArray *)ar {
for(NSDictionary *dInner in ar) {
NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
NSArray *arInner = [dInner valueForKey:#"Objects"];
if (arInner && [arInner count] > 0) {
[self miniMizeThisRows:arInner];
}
if ([arraylist indexOfObjectIdenticalTo:dInner]!= NSNotFound) {
[arraylist removeObjectIdenticalTo:dInner];
[tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (void)viewDidLoad {
[super viewDidLoad];
dictBtnStatus = [[NSMutableDictionary alloc] init];
arraylist = [[NSMutableArray alloc] init];
array5 = [[NSMutableArray alloc] init];
array1 = [[NSMutableArray alloc] init];
objects = #[#"Medical Services In Chicago", #"Medical Services On Campus", #"ABC", #"California"];
// objects = #[#"Medical Services In Chicago", #"Medical Services On Campus"];
arrObjectValueChicago = #[#"Ronald McDonald® Children's Hospital of Loyola", #"Burn Centers", #"Gottlieb Hospitals"];
arrObjectValueCampus = #[#"Cardinal Bernardin Cancer Center1", #"Center for Heart & Vascular Medicine2", #"ABC"];
for (int i = 0; i < [arrObjectValueCampus count]; i++) {
dictListCampus1 = [[NSDictionary alloc]initWithObjectsAndKeys:#"2", #"level",[arrObjectValueCampus objectAtIndex:i], #"name", nil];
[array5 addObject:dictListCampus1];
}
NSDictionary *dictListCampus = [[NSDictionary alloc]initWithObjectsAndKeys:#"Wellness Centers", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:dictListCampus, nil];
NSDictionary *dictListCampus2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Wellness Centers123", #"name", #"1", #"level", array5, #"Objects", nil];
NSMutableArray *array61 = [[NSMutableArray alloc] initWithObjects:dictListCampus2, nil];
array3 = [[NSMutableArray alloc] init];
for (int i = 0; i < [arrObjectValueChicago count]; i++) {
dictList3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"2", #"level",[arrObjectValueChicago objectAtIndex:i], #"name", nil];
[array3 addObject:dictList3];
}
NSDictionary *dictList2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Hospitals", #"name", #"1", #"level", array3, #"Objects", nil];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:dictList2, nil];
for (int i = 0; i < [objects count]; i++) {
if (i == 0) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:0], #"name", #"0", #"level", array2, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 1) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:1], #"name", #"0", #"level", array6, #"Objects", nil];
[array1 addObject:dictionary];
} else if (i == 2) {
dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:2], #"name", #"0", #"level", array61, #"Objects", nil];
[array1 addObject:dictionary];
}
// else {
// dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:[objects objectAtIndex:3], #"name", #"0", #"level", array2, #"Objects", nil];
// [array1 addObject:dictionary];
//}
}
dictList = [[NSDictionary alloc] initWithObjectsAndKeys:array1, #"Objects", nil];
NSLog(#"DictList :: %#", dictList);
arrayOriginal = [dictList valueForKey:#"Objects"];
NSLog(#"Array Original :: %#", arrayOriginal);
[arraylist addObjectsFromArray:arrayOriginal];
//[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//[tblist.delegate tableView:tblist didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
}
#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arraylist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//static NSString *CellIdentifier = #"CustomCellIdentifier";
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (IndentLevel == 0) {
CustomCellHeader *cell = (CustomCellHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellHeader" owner:self options:nil];
cell = self.headercell;
self.headercell = nil;
}
cell.lblHeader.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
NSLog(#"indexFor level 0 ::%# %d",cell.lblHeader.text, indexPath.row);
return cell;
} else if (IndentLevel == 1) {
CustomCellSubHeader *cell = (CustomCellSubHeader *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (!cell) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCellSubHeader" owner:self options:nil];
cell = self.subheadercell;
self.subheadercell = nil;
NSLog(#"dicbtn %#", dictBtnStatus);
}
NSString *strName = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
NSString *str = [dictBtnStatus objectForKey:strName];
NSLog(#"indexFor level 1 ::%# %d", strName, indexPath.row);
if ([str isEqualToString:#"1"]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
}
// else {
// [cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
//}
cell.lblSubHeader.text = strName;
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
if (indexPath.row == arraylist.count - 1) {
cell.imgShadow.hidden = NO;
}
return cell;
} else if (IndentLevel == 2) {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell = self.cells;
self.cells = nil;
}
cell.txtAddress.text = [[arraylist objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell.btnCall addTarget:self action:#selector(btnCall:) forControlEvents:UIControlEventTouchUpInside];
[cell setIndentationLevel:[[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCellSubHeader *cell;
if ([[tblist cellForRowAtIndexPath:indexPath] isKindOfClass:[CustomCellSubHeader class]]) {
cell = (CustomCellSubHeader*)[tblist cellForRowAtIndexPath:indexPath];
UIImage *img = [cell.btnarrow imageForState:UIControlStateNormal];
if ([img isEqual:[UIImage imageNamed:#"dwn1_arow.png"]]) {
[cell.btnarrow setImage:[UIImage imageNamed:#"gry_arrow.png"] forState:UIControlStateNormal];
[dictBtnStatus setValue:#"0" forKey:cell.lblSubHeader.text];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_default.png"]];
cell.imgShadow.hidden = NO;
} else {
[cell.btnarrow setImage:[UIImage imageNamed:#"dwn1_arow.png"] forState:UIControlStateNormal];
dictBtnStatus = [[NSMutableDictionary alloc] init];
[dictBtnStatus setValue:#"1" forKey:cell.lblSubHeader.text];
[cell.imgSubHeader setImage:[UIImage imageNamed:#"tab_active.png"]];
cell.imgShadow.hidden = YES;
}
}
NSMutableArray *arrIndex = [[NSMutableArray alloc] init];
NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (IndentLevel == 0) {
arrLast = [[NSMutableArray alloc] init];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *d = [arraylist objectAtIndex:indexPath.row];
if ([d valueForKey:#"Objects"]) {
NSArray *ar = [d valueForKey:#"Objects"];
BOOL isAlreadyInserted = NO;
for (NSDictionary *dInner in ar) {
NSInteger index = [arraylist indexOfObjectIdenticalTo:dInner];
isAlreadyInserted = (index > 0 && index != NSIntegerMax);
if (isAlreadyInserted) break;
}
if (isAlreadyInserted) {
[self miniMizeThisRows:ar];
} else {
NSUInteger count = indexPath.row + 1;
NSMutableArray *arCells = [NSMutableArray array];
for (NSDictionary *dInner in ar) {
[arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[arrLast addObject:dInner];
[arraylist insertObject:dInner atIndex:count++];
}
[tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationBottom];
}
}
//NSUInteger IndentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (IndentLevel == 0) {
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
for (int i = 0 ; i < arraylist.count; i++) {
NSDictionary *dic = [arraylist objectAtIndex:i];
if ([[dic valueForKey:#"level"] intValue] != 0) {
NSInteger index = [arrLast indexOfObjectIdenticalTo:dic];
if ((index >= 0 && index != NSIntegerMax)) {
} else {
[discardedItems addIndex:i];
[arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
}
if (discardedItems.count > 0) {
[arraylist removeObjectsAtIndexes:discardedItems];
[tblist deleteRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger indentLevel = [[[arraylist objectAtIndex:indexPath.row] valueForKey:#"level"] intValue];
if (indentLevel == 0) {
return 40;
} else if (indentLevel == 1) {
return 25;
} else if (indentLevel == 2) {
CustomCell *cell = ((CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"]);
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Arial" size:11.5];
CGSize myStringSize = [cell.txtAddress.text sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
if (myStringSize.height > 80) {
myStringSize.height = 50;
[cell.txtAddress setFrame:CGRectMake(cell.txtAddress.frame.origin.x, cell.txtAddress.frame.origin.y, cell.txtAddress.frame.size.width, myStringSize.height)];
cell.txtAddress.scrollEnabled = YES;
} else {
cell.txtAddress.scrollEnabled = YES;
myStringSize.height = 40;
}
return myStringSize.height + 50;
} else return 25;
}
#pragma mark - TableAnimation
- (void)miniMizeThisRows:(NSArray *)ar {
for (NSDictionary *dInner in ar) {
NSUInteger indexToRemove = [arraylist indexOfObjectIdenticalTo:dInner];
NSArray *arInner=[dInner valueForKey:#"Objects"];
if (arInner && [arInner count] > 0) {
[self miniMizeThisRows:arInner];
}
if ([arraylist indexOfObjectIdenticalTo:dInner] != NSNotFound) {
[arraylist removeObjectIdenticalTo:dInner];
[tblist deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
#pragma mark - Buttons
- (IBAction)btnCall:(id)sender {
UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Do you want to call on this number." delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
Notpermitted.delegate = self;
[Notpermitted tag];
[Notpermitted show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *strCall = [NSString stringWithFormat:#"tel://999-999-9999"];
NSURL *url = [NSURL URLWithString:strCall];
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:#"iPhone"] ) {
[[UIApplication sharedApplication] openURL:url];
} else {
UIAlertView *Notpermitted = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Your device doesn't support this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[Notpermitted show];
}
}
}
see following tutorial
http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
www.alexandre-gomes.com/?p=482
Expand/collapse section in UITableView in iOS

UITableView gets Slower with every new NSMutableArray

I have a UITableView that populates the results of a search that the user looks up. In order to do this I am using a NSMutableArray of Dictionaries where objects are added for the first 10, and then when the user scrolls to the bottom it populates the next 10 until there are no results left to show.
This all works well and good but I started to notice that the more searches that are done, the slower the table gets. Here is some of the code:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.objectsArray removeAllObjects];
[self.objectsArray setArray:nil];
[itemsTable reloadData];
[itemsTable scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:false];
[self loadItemsFromURL:searchURL withItemDescription:encodedString atStartRow:start andEndRow:end];
}
The above is when a new search is performed. It then does a NSURLConnection and responds with this:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (self.objectsArray == nil)
self.objectsArray = [NSMutableArray array];
// self.objectsArray = [[NSMutableArray alloc] init];
NSError *error;
NSDictionary *returnArray = [[NSJSONSerialization JSONObjectWithData:itemsData options:kNilOptions error:&error] valueForKey:#"items"];
for (id key in returnArray)
{
[self.objectsArray addObject:[returnArray objectForKey:key]];
}
counter += 10;
[itemsTable reloadData];
}
As you can see, if a user conducts a new search all objects are removed with [self.objectsArray removeAllObjects]and I even try to set the array to nil. If I perform multiple searches the UITableView gets slower and slower with scrolling each time. It is almost like the controller sees the array as getting larger and larger with each search even though I am removing all of the objects from it before the search. Any ideas or am I going about this the wrong way?
EDIT:
Here is the cellForRowAtIndexPath: method. cell is a subclassed UITableViewCell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Product Cell";
static NSString *LoadCellIdentifier = #"Loading Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([self.objectsArray count] <= 0 )
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.itemName.text = #"No items found.";
cell.itemPrice.text = #"";
cell.itemLocation.text = #"";
cell.addButton.hidden = YES;
}
else
{
if ([indexPath row] == [self.objectsArray count])
{
if ( [self.objectsArray count] >= 10 )
{
if ( [self.objectsArray count] < counter)
{
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
[cell.loadingSpinner stopAnimating];
cell.itemName.text = #"No more items found.";
}
else
{
if (!running)
{
[self loadItemsFromURL:searchURL withItemDescription:encodedString atStartRow:[self.objectsArray count] + 1 andEndRow:[self.objectsArray count] + 10];
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
cell.itemName.text = #"Loading more items...";
[cell.loadingSpinner startAnimating];
running = true;
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
[cell.loadingSpinner startAnimating];
}
}
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *match = [self.objectsArray objectAtIndex:[indexPath row]];
cell.addButton.hidden = NO;
if ([match valueForKey:#"DESCRIPTION"] == [NSNull null] )
{
cell.itemName.text = #"Description not available.";
}
else
{
cell.itemName.text = [match valueForKey:#"DESCRIPTION"];
}
if ([match valueForKey:#"AD"] != [NSNull null])
{
NSMutableString *adString = [NSMutableString stringWithString:[match valueForKey:#"AD"]];
NSRange textRange;
textRange = [adString rangeOfString:#"1/"];
if (textRange.location != NSNotFound)
{
[adString replaceCharactersInRange:[adString rangeOfString:#"1/"] withString:#"$"];
}
else
{
[adString replaceCharactersInRange:[adString rangeOfString:#"/"] withString:#"/$"];
}
cell.itemPrice.text = adString;
}
else if ([match valueForKey:#"REGULAR"] == [NSNull null])
{
cell.itemPrice.text = #"$ N/A";
}
else
{
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *price = [NSNumber numberWithDouble:[[match valueForKey:#"REGULAR"] doubleValue]];
NSString *stringPrice = [currencyStyle stringFromNumber:price];
cell.itemPrice.text = [NSString stringWithFormat:#"%#", stringPrice];
}
if ([match valueForKey:#"AISLE"] == [NSNull null])
{
cell.itemLocation.text = #"Item location: N/A";
}
else
{
cell.itemLocation.text = [NSString stringWithFormat:#"Item Location: %#", [match valueForKey:#"AISLE"]];
}
match = nil;
}
}
return cell;
}
EDIT 2:
Here is a snippet of what the JSON looks like:
{
items = {
263149 = {
AD = "###";
AISLE = 6A;
DESCRIPTION = "Cinnamon Toasters";
R = 9;
REGULAR = "#.##";
};
26599 = {
AD = "####";
AISLE = 6A;
DESCRIPTION = "Quaker Life Cereal";
R = 2;
REGULAR = "#.##";
};
40517 = {
AD = "###";
AISLE = 6A;
DESCRIPTION = "Toasted Oats";
R = 1;
REGULAR = "#.##";
};
};
};
Ok, I think your problem is the excessive creation of Array objects. So do the following instead of you're array creation:
NSDictionary *returnArray = [[NSJSONSerialization JSONObjectWithData:itemsData options:kNilOptions error:&error] valueForKey:#"items"];
for (NSDictionary *dict in returnArray in returnArray)
{
[self.objectsArray addObject:dict];
}
counter += 10;
[itemsTable reloadData];
What you'll get as you see is an array of NSDictionary objects, your return array is already an NSDictionary of dictionary objects. Also, slight observation, where are you resetting your counter?
EDIT: creating the NSDictionary from NSData:
[NSJSONSerialization JSONObjectWithData:self.requestData options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error]
The requestData is generated using these delegate methods:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"In didReceiveResponse");
[self.requestData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"In didReceiveData");
[self.requestData appendData:data];
}
I was able to find the issue in one line in the cellForRowAtIndexPath:. I commented out: cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; at the top and made sure it was only called once. I also did do a bit of cleanup as suggested by 8vius and now only once NSString is being allocated in that method call. Once I did these two things it was nice and responsive again without any stutters.

Resources