didselectrowatindexpath not called - ios

I am facing a problem where the didselectrowatindexpath is not getting called.
My tableview is set in viewdidload as follows:
- (void)viewDidLoad
{
//[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
self.detailViewController = (klViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
Now my cellForRowAtIndexPath gets called and I am reusing the cells as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"menuSelection1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = data.treeName;
return cell;
}
I have checked other answers and mostly said that there could be the use of didDeselectRowAtIndexPath might be used. But this is not the case here.
Now my problem is in my split view controller whenever I select any option the corresponding detail view doesn't get displayed and is blank. None of my methods in the rootview controller (klViewController) gets called. What could be the reason for this?
Following are the all methods related to UITableView. It is implemented in the same class as in viewDidLoad
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.LoadMenuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"menuSelection1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = data.treeName;
return cell;
}
-(NSArray *)LoadMenuItems
{
NSMutableArray * menuData = [[NSMutableArray alloc] initWithCapacity:5];
AmbassadorInfoData * data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Ambassador Info";
data.treeDescription = #"This is temp data.";
NSString * file = [[NSBundle mainBundle] pathForResource:#"oak" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"AmbassadorInternalList";
data.treeDescription = #"This is temp data.).";
file = [[NSBundle mainBundle] pathForResource:#"DouglasFir" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 3";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"SugarMaple" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 4";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"RedMaple" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 5";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"Pine" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
menuItems = [[NSArray alloc] initWithArray:(NSArray *)menuData];
data=nil;
return menuItems;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:[self.tableView indexPathForSelectedRow].row];
detailObject = selectedItem;
[detailViewController setDetailItem:detailObject];
}
#end

It could be that your program is stuck in a long running operation because of the way you're using LoadMenuItems. Every time numberOfRowsInSection and cellForRowAtIndexPath is called, you're recreating the entire array -- this is bad. You should call LoadMenuItems once, in viewDidLoad perhaps, and once you've created that array, use it, not a call to LoadMenuItems when you need to get data out. I don't know if this is what is causing your problem, but you should definitely fix it, and see if it makes a difference.

Try these modifications:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"TableView methods are called! problem is getting AmbassadorInfoData object")
AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:indexPath.row];
if (!selectedItem) NSLog(#"Unable to get Ambassador object, check your menuItems");
[self.detailViewController setDetailItem:selectedItem]; //notice "self."
}
also, uncomment [super viewDidLoad];
EDIT:
it appears that you are using the code you don't understand, in this case i am suggesting to learn some basics....here's a great site that helped me understand things quite a few times
and this is a guide from Apple: Table View Programming Guide

Related

Dynamic UITableViewCell height for a cell that contains a UIWebView in iOS

What I'm trying to do is load some HTML resource files in to UITableView cells. I wanted to adjust the cell heights depend on the content size of the UIWebView. Following is some of my test codes.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
NSString *htmlFileName = [NSString stringWithFormat:#"page%ld", (long)indexPath.row];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
return cell;
}
If I describe my CustomTableViewCell.xib, it has a UIWebView inside the Content View and UIWebView has following constraints given.
ContentView.Bottom = WebView.Bottom + 8
ContentView.Top = WebView.Top +8
ContentView.Leading = WebView.Leading + 8
ContentView.Trailing = WebView.Trailing + 8
HTML fils has no JavaScript or CSS content. Simple as below.
<html>
<header>
</header>
<body>
<p>eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee</p>
</body>
</html>
Now my out put is like below image.
As I explained at top, my web pages doesn't contain any JavaScript. So I may not able to use stringByEvaluatingJavaScriptFromString method to return the text.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
arrOfHTMLList= [[NSMutableArray alloc] init];
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"aaaaaaaaaa aaaaaa aaaaaaa aaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaa" forKey:#"htmlString"];
NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"abababababbabababababab ababab bababa ababababb" forKey:#"htmlString"];
NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:#"htmlString"];
NSMutableDictionary *dic4 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:#"htmlString"];
[arrOfHTMLList addObject:dic1];
[arrOfHTMLList addObject:dic2];
[arrOfHTMLList addObject:dic3];
[arrOfHTMLList addObject:dic4];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UIWebView *webView = (UIWebView*)[cell viewWithTag:1];
NSString *myHTML = arrOfHTMLList[indexPath.section][#"htmlString"];
[webView loadHTMLString:myHTML baseURL:nil];
webView.delegate = self;
webView.frame = CGRectMake(0, 0, screenWidth,cell.frame.size.height);
return cell;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
for (UIView *parent=[webView superview];parent!=nil ;parent=[parent superview] )
{
if([parent isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell=(UITableViewCell *)parent;
NSIndexPath *indexPath=[_tableView indexPathForCell:cell];
int height = webView.scrollView.contentSize.height;
[[arrOfHTMLList objectAtIndex:indexPath.section] setObject:[NSString stringWithFormat:#"%d",height] forKey:#"height"];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [arrOfHTMLList[indexPath.section][#"height"] intValue];
return height;
}
Hope it helps.
the main issue is to get a web view's frame to adapt to its html content right away :)
and THAT only happens after it asynchronously loaded the content!
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
//TODO: update the table's height for the cell for the webview
}
a drop-in solution I wrote is a custom UITableViewCell subclass that has a webview and informs a delegate to reload the table:
https://github.com/Daij-Djan/DDUtils/tree/master/objC/DDUtils-IOS/ui/M42WebviewTableViewCell%20%5Bios%5D
the most important piece of code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
M42WebviewTableViewCell *cell = _loadedCells[#(indexPath.row)];
if (!cell) {
cell = [[M42WebviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *object = _objects[indexPath.row];
cell.tag = indexPath.row;
cell.delegate = self;
[cell setHtml:object AndBaseURL:nil];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//if a cell isnt ready, it wont be there and the method will return 0
M42WebviewTableViewCell *cell = _loadedCells[#(indexPath.row)];
return cell.height;
}
#pragma mark - M42WebviewTableViewCellDelegate
- (void)tableCellDidLoadContent:(M42WebviewTableViewCell *)cell {
if (!_loadedCells) {
_loadedCells = [[NSMutableDictionary alloc] init];
}
[_loadedCells setObject:cell forKey:#(cell.tag)];
//only refresh sizes of rows
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
I came up with my own solution. But thanks all the others who commented on this question and open my mind in different paths. So here what I did.
First added a UIWebViewDelegate to my ViewController class
#interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate> {
Then loading HTML pages and adding delegate is done inside following method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.webView.delegate = self;
cell.webView.tag = indexPath.row;
cell.webView.scrollView.scrollEnabled = NO;
NSString *htmlFileName = [NSString stringWithFormat:#"page%ld", (long)indexPath.row];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
return cell;
}
After web view is loaded, calculate the size that fits to it's content and after all web views are loaded, I reload table data like below.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView.tag == 0) {
[cellHeightMutArray removeAllObjects];
}
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
[cellHeightMutArray insertObject:#(fittingSize.height) atIndex:webView.tag];
if (((webView.tag + 1) == dataArray.count) && !reloadedTableViewAfterLoadingWebContent) {
reloadedTableViewAfterLoadingWebContent = YES;
_tableView.hidden = NO;
[_tableView reloadData];
}
}
Then I adjust the height of the cell (which was the main issue that I had)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (cellHeightMutArray.count > 0) {
return [[cellHeightMutArray objectAtIndex:indexPath.row] intValue];
}
return 44;
}
This is my solution and it works perfectly in iPhones and iPads.
But I had different issue when I run it with more complex user interface in iPads which is in my real project. I'll update this if my solutions for those issue related to this question.

UITable Cell info only showing when row is selected

I'm getting some really odd behaviour with a UITableView i'm working on. I have the following code, which loads data from an array into a UITableView. The code seems to work... but the labels only show values when i click on each row. I'm stumped as to why this is happening as i'm pretty sure i didnt have this issue before... but i could be wrong. My code:
#interface TableViewController ()
#end
#implementation TableViewController
//#synthesize cRef;
-(void) getData:(NSData *) data {
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self.tableView reloadData];
}
-(void) start {
NSURL *url = [NSURL URLWithString: URL];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self start];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [json count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:#"CountryName"];
cell.detailTextLabel.text = [info objectForKey:#"id"];
return cell;
}
Any help appreciated.
As #ktpatel said try changing font color
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:#"CountryName"];
cell.detailTextLabel.text = [info objectForKey:#"id"];
// set font color
cell.textLabel.textColor = [UIColor redColor];
cell.detailTextLabel.textColor = [UIColor redColor];
if ([cell isSelected]) {
// make changes for selected state
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor blackColor];
}
return cell;
}
Also I don't see your code for didSelectRowAtIndexPath, I think you must be doing this in it to see the selected state of UITableViewCell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor blackColor];
}

if-statement ios check json parameter

I'm currently parsing a JSON-feed to my table view in my iOS app. In the JSON-feed there's two different types of posts: "type":"post" and "type":"shared".
When I click on a cell in the Table View, a new view opens with the full posts etc. What I need is to set an if-statement in the didSelectRowAtIndexPath, saying: "If type = post, open this view. Else if type = shared, open this view". So I need to somehow check the json-parameter type. And based on that go to the two different views.
The didSelectRowAtIndexPath method currently looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//getting the data I'm passing to the next view, where movies = NSMutableArray
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = movie;
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
And the full code for parsing the data looks like this:
#implementation HomeViewController
#synthesize profilePic;
#synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
#synthesize btnFaceBook, btnTwitter, btnTwitter2;
#synthesize strURLToLoad;
#synthesize movies;
#synthesize statsHome;
#synthesize fontForCellText;
#synthesize twitterFollowers;
#synthesize facebookLikes;
#synthesize instagramFollowers;
- (void)viewDidLoad
{
[super viewDidLoad];
// Initializing Data Source
movies = [[NSMutableArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"link.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.movies = [[NSArray alloc] initWithArray:JSON];
[self.activityIndicatorView stopAnimating];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return movies.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"HomeFeedView";
HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HomeFeedView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:#"message"];
cell.published.text = [movie objectForKey:#"published"];
cell.twitterName.text = [movie objectForKey:#"celebname"];
return cell;
}
- (NSString *)getTextKey
{
return #"message";
}
- (NSString *)getPostedTime
{
return #"published";
}
- (NSString *)getTwitterName
{
return #"celebname";
}
- (CGFloat)getHeightForText:(NSString *)strText
{
CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
NSLog(#"labelSize.height = %f",labelSize.height);
return labelSize.height;
}
#end
JSON structure/data:
{
"message":"blablablabla",
"published":"5 hours ago",
"link":"http://google.com",
"unix":1395493814,
"name":"Name",
"story":null,
"picture":"some-pic.jpg",
"type":"shared"
},
{
"message":"blablabla",
"published":"5 hours ago",
"name":"Name",
"unix":1395493814,
"type":"post"
},
In your HomeFeedView.h just add a new property say type.
#property(strong, nonatomic) NSString *type;
Then set this type property in cellForRowAtIndexPath delegate method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"HomeFeedView";
HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HomeFeedView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:#"message"];
cell.published.text = [movie objectForKey:#"published"];
cell.twitterName.text = [movie objectForKey:#"celebname"];
cell.type = [movie objectForKey:#"type"];
return cell;
}
and didSelect method write this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//getting the data I'm passing to the next view, where movies = NSMutableArray
HomeFeedView *cell = [tableview cellForRowAtIndexPath:indexPath];
if([cell.type isEqualToString: #"post"]){
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = [self.movies objectAtIndex:indexPath.row];
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
else{
// Allocating the Second View I want to open
HomePostsObject *newView = [[HomePostsObject alloc] init];
// passing the data to newView
newView.theMovie = [self.movies objectAtIndex:indexPath.row];
// presenting the view
[self.navigationController pushViewController:newView animated:YES];
}
}

Data Lost after scroll in UITableView

I am trying to display plist data in tableview.
My problem is that after scrolling down i lost my previously fetched data.
Actually i am using custom cell so i think this is happen.
Code for Source and delegate of table is below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"Cell";
OffersCustomCell *cell = (OffersCustomCell *)[mytableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"OffersList" ofType:#"plist"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OffersCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.titleLabel.font = [UIFont fontWithName:#"Formata-Regular" size:13.0f];
cell.saveLabel.font = [UIFont fontWithName:#"Formata-Bold" size:14.0f];
cell.nowLabel.font = cell.titleLabel.font;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
NSLog(#"%#",dict);
cell.titleLabel.text = [dict objectForKey:#"title"];
cell.nowLabel.text = [dict objectForKey:#"price"];
cell.saveLabel.text = [dict objectForKey:#"rondel"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[mytableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you set the height for each cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
//calculate height for each cell
return height;
}
Use this UITableView delegate method to set height of the cells.
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
I solved it. :)
only problem is no retaining memory for UItableview.
tableData = [[NSArray arrayWithObjects:[dic objectForKey:#"Response"], nil] retain];

Reload UITableView data not working properly

I have an array objects received from my webservice that I use to populate a UITableView. I'm trying to implement a method that gets a new array from the webservice and redefines my array that populates the table and then reload the tableView to use the new objects from this array.
This is the method that should do the work:
WSCaller *caller = [[WSCaller alloc]init];
arrayFromWS = [caller getArray];
[self.table reloadData];
and it doesn't work. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"productsCellIdentifier";
ProductsCell *cell = nil;
cell = (ProductsCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:#"ProductsCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[ProductsCell class]])
{
cell = (ProductsCell *)currentObject;
break;
}
}
}
if (productsMutableArray)
{
cell.backgroundColor = [UIColor whiteColor];
cell.productName.text = [[self.productsMutableArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [productsMutableArray count];
}
Your datasource implementation is wrong (or at least the snippets are).
First, you aren't retaining the arrayFromWS:
WSCaller *caller = [[WSCaller alloc] init];
arrayFromWS = [caller getArray];
[self.table reloadData];
Second, on your tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: methods, you're using a different array (productsMutableArray).
To fix that, I'd recommend to change the code above for something like (assuming that your productsMutableArray is a strong/retain property:
WSCaller *caller = [[WSCaller alloc] init];
self.productsMutableArray = [NSMutableArray arrayWithArray:[caller getArray]];
[self.table reloadData];

Resources