Same UITableViewCell when using JSON - ios

When my UITableview loads I am getting the same cell over and over. I'm assuming I need to add something about indexpath.row but this is my first time using JSON so I'm not sure exactly how to do it. Attached is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
if ([subArray count]) {
for (NSDictionary *dictItem in subArray) {
cell.titleLabel.text = [dictItem objectForKey:#"title"];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
NSString *separatorString = #"T";
NSString *firstUploadString = [dictItem objectForKey:#"uploaded"];
NSString *uploadString = [firstUploadString componentsSeparatedByString:separatorString].firstObject;
cell.dateUploadLabel.text = uploadString;
NSString *viewCount = [NSString stringWithFormat:#"%#",[dictItem objectForKey:#"viewCount"]];
cell.viewsLabel.text = viewCount;
NSDictionary *playBackDict = [dictItem objectForKey:#"player"];
NSString *playBackURL = [playBackDict objectForKey:#"mobile"];
NSLog(playBackURL);
NSDictionary *thumbnailDict = [dictItem objectForKey:#"thumbnail"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *thumbnailURL = [thumbnailDict objectForKey:#"sqDefault"];
NSURL *url = [NSURL URLWithString:thumbnailURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// Here in main thread set image to your cell imageView.
// e.g.
[cell.videoThumbnail setImage:thumbNailImage];
});
});
};
}
return cell;
}
I'm just having trouble iterating through these so any help would be awesome. Thanks!!

You are iterating over the subArray variable and configuring your cell for each item in the array. So all your cells always are going to have the same data that it correspondes with the last item of your array.
You need to use the indexPath parameters that receive in the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to get the correct object for each cell, similar to this snippet:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell... if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
// You should check here the array size before get the item
NSDictionary *dictItem = [subArray objectAtIndex:indexPath.row];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
// ToDo: all your configuration here using dictItem
return cell;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called for the tableView one time for each row depending on the value returned by (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView and (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Related

XML Parsing getting id

how to get all id's from this url and display in uitableview "http://www.thomas-bayer.com/sqlrest/CUSTOMER/".
- (void)viewDidLoad
{
NSURL *URL = [[NSURL alloc] initWithString:#"http://www.thomas-bayer.com/sqlrest/CUSTOMER/"];
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(#"dictionary: %#", xmlDoc);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlDoc count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [[[xmlDoc objectForKey:#"CUSTOMER"]objectAtIndex:indexPath.row]objectForKey:#"__text"];
return cell;
}
I am getting only 0,1,2 values displayed in uitableview and ignoring the remaining id's ?
Use this Library to convert XMLData into NSDictionary object.
https://github.com/nicklockwood/XMLDictionary then simply fetch your required data from NSDictionary object and update it into your TableView.

Get cell value in tableView

I want to get cell value in tableView by using:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"selected cell: %#", selectedCell);
}
It should be a String but this is what i get in the NSLog:
selected cell: <RewardCategoriesTableViewCell: 0x7ae11290; baseClass = UITableViewCell; frame = (0 92.01; 320 44); autoresize = W; layer = <CALayer: 0x7ae11450>>
And this is my Data Source of Array:
NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil];
return data;
This is cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.section >= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.section];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.section];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Thanks for helps.
In your way, you get the UITableviewCell or subclass
object,so what you log is right.
If you want to get cell value,you should check your dataSource,in
MVC pattern that is your Model.
For example: you have a array named dataArray as Model,Then you get
value dataArray[indexPath.row]
I am not sure why you set text of cell based on the indexPath.section,so ,I post example code based on the code you post
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.section];
NSString * cellText = category.CategoryName;
}
BTY,I think the right way is let text of cell based on the row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.row>= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.row];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.row];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Then in:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.row];
NSString * cellText = category.CategoryName;
}

String becomes blank when didSelectRowAtIndexPath called

I have a custom cell with a property that is an NSString called videoURl. My implementation code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
// Configure the cell...
cell.videoThumbnail.image = [UIImage imageNamed:[thumbNailArray objectAtIndex:indexPath.row]];
NSString *stringWithoutPng = [[thumbNailArray objectAtIndex:indexPath.row]
stringByReplacingOccurrencesOfString:#".png" withString:#""];
cell.titleLabel.text = stringWithoutPng;
cell.dateUploadLabel.text = [videoUploadDateArray objectAtIndex:indexPath.row];
cell.durationLabel.text = [videoLengthArray objectAtIndex:indexPath.row];
cell.viewsLabel.text = [videoViewsArray objectAtIndex:indexPath.row];
cell.videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(cell.videoURL);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RemedyYouTubeTableViewCell *cell = (RemedyYouTubeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *videoURL = cell.videoURL;
NSLog(videoURL);
NSLog(#"^videourl");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
When it logs the urls at the top everything is fine but when it is called at the bottom nothing prints. What am i doing wrong? Or is there a better way to do it? Thanks!
please replace your didSelectRowAtIndexPath method with this given code...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(videoURL);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}

How do you load multiple arrays into a UITableView?

I am calling some JSON and loading a table with the data from a single array. That's working great. Now I'm trying to figure out
A. the best way to load the data into the table and
B. the best way to section that data off.
This is my 6th week of iOS development and I am pretty new. I have a fairly weak Javascript background.
My first (failed attempt) way to concatenate the arrays together and pass that to the tableview. I think this is wrong for multiple reasons (issues with sectioning afterwards, know "which" one to delete, etc). Any help is greatly appreciated!
Didn't work:
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:#"all_groups"]; //NSDictionary
self.firstGroup = [self.allGroups objectForKey:#"first_group"]; //NSMutableArray
self.secondGroup = [self.allGroups objectForKey:#"second_group"]; //NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:#"third_group"]; //NSMutableArray
NSMutableArray *allGroupsArray = [self.firstGroup arrayByAddingObjectsInArray:[self.secondGroup arrayByAddingObjectsInArray:self.thirdGroup]];
Does work now, but can't figure out multiple arrays into the tableview:
-(void) getTheData {
NSString *sessionToken = [[AFOAuthCredential retrieveCredentialWithIdentifier:#"myToken"] accessToken];
if (sessionToken == nil) {
LoginViewController *loginView = [[LoginViewController alloc] init];
[self presentViewController:loginView animated:NO completion:nil];
return;
}
NSURL *url = [NSURL URLWithString:#"https://greatwebsitetogetdata.com"];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:#"MY_CLIENT" secret:#"1234567890abc"];
[oauthClient getPath:#"/api/v1/json" parameters:#{#"access_token": sessionToken} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *errorJson = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&errorJson];
self.allGroups = [dataDictionary objectForKey:#"all_groups"]; //This is a NSDictionary
self.firstGroup = [self.allGroups objectForKey:#"first_group"]; //This is a NSMutableArray
self.secondGroup = [self.allGroups objectForKey:#"second_group"]; //This is a NSMutableArray
self.thirdGroup = [self.allGroups objectForKey:#"third_group"]; //This is a NSMutableArray
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.firstGroup.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *groupInfo = self.firstGroup[indexPath.row];
static NSString *cellIdentifier = #"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[#"title"];
cell.groupLikes.text= [NSString stringWithFormat:#"%#", groupInfo[#"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:#"%# - %#", groupInfo[#"start_date"], groupInfo[#"end_date"]];
cell.groupAcceptance.text= groupInfo[#"acceptance_type"];
return cell;
}
I think an array of arrays would work better for you, where each array represents a section. allGroups should then contain 3 arrays.
Then you need to override the datasource method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.allGroups.count;
}
and then in:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allGroups[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *group = self.allGroups[indexPath.section];
NSDictionary *groupInfo = group[indexPath.row];
static NSString *cellIdentifier = #"Cell";
groupTableViewCell *cell = (groupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[groupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.groupTitle.text= groupInfo[#"title"];
cell.groupLikes.text= [NSString stringWithFormat:#"%#", groupInfo[#"likes"]];
cell.groupRunDates.text= [NSString stringWithFormat:#"%# - %#", groupInfo[#"start_date"], groupInfo[#"end_date"]];
cell.groupAcceptance.text= groupInfo[#"acceptance_type"];
return cell;
}

First 3 cells are the same but turn correct once I start scrolling?

So Whenever I launch my app it looks like this and the first 3 cells are all the same. But once I start scrolling up and down it fixes and cell 2 and 3 show the correct information.
This is currently how my code looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyFeedCell";
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SampleTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SampleTableCell *)currentObject;
break;
}
}
}
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here's a gif of how it's currently functioning
http://gyazo.com/2011099496257445c008a717beabd8fd
The whole topLevelObjects gambit is no longer necessary, replace your code with this and see if your still getting the problem:
//this above #interface
static NSString *CellIdentifier = #"MyFeedCell";
//Put this in viewDidLoad
[self.table registerClass:[SampleTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.table registerNib:[UINib nibWithNibName:#"SampleTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Resources