Program crashes when application becomes active from background and UItableView is reloaded - ios

Hi Guys I am creating a iOS(Universal) application.
My problem is that in my application there is a UITableView that works fine if the application doesn't goes to background(By pressing home button or by clicking a URL link).
If the application goes into the background, when it gets active, reloading a cell or the whole table view crashes the whole application.
Any Solution???
Is removing the dequeueReusableCellWithIdentifier: will make some difference??
The code is :
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
Animation *animation = [animationArray objectAtIndex:indexPath.row];
cell.textLabel.text = animation.title;
if(animation.isAnimationEnabled){
cell.imageView.image = OnImage;
}else {
cell.imageView.image = OffImage;
}
return cell;
}

You have to make sure all the view controllers in your app can survive arbitrary number of cycles of view load/view unload. Check whether you remove something in viewDidUnload that is used in viewDidLoad.

Related

tableview viewWithTag not retrieving UIElements objective-c

I am doing using some code that I have seen work before. Essentially a user answers yes or no on a post with some buttons. Pressing yes or no updates the database, which is working correctly, and it also updates the visible UI, which is not working. This UI updates the buttons so they one is selected, other is highlighted and both are disabled for user interaction. Also it makes changes to two UILabels. The method that these buttons calls needs to update the database and retrieve the buttons from the tableViewCell and update the changes I have the methods working in another ViewController so I can not understand the difference here. Here is my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%ld,%ld",(long)indexPath.section,(long)indexPath.row];
NSLog(#" simple: %#",simpleTableIdentifier);
if (indexPath.row==0) {
ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell = [self createProfileCell:cell];
return cell;
}else{
YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil) {
cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell = [self createYesNoCell:cell:indexPath];
return cell;
}
}
Essentially what this does is create the users profile in the first cell, and load all the questions that user asks. This is the major difference I see between the old tableView and this tableView. In createYesNoCell I create the UIElements and create tags as follows
cell.yesVoteButton.tag=indexPath.row+ yesVoteButtonTag1;
cell.noVoteButton.tag=indexPath.row+ noVoteButtonTag1;
cell.yesCountLabel.tag=indexPath.row+ yesCountLabelTag1;
cell.noCountLabel.tag=indexPath.row+ noCountLabelTag1;
The buttons have the selector that initiates a number of things. It finds which button was pressed by the following.
NSInteger index;
if(sender.tag>=yesVoteButtonTag1){
NSLog(#"Yes button pressed");
votedYes=true;
index=sender.tag-yesVoteButtonTag1;
}else{
NSLog(#"No button Pressed");
votedYes=false;
index=sender.tag-noVoteButtonTag1;
}
UILabel *yesLabel = (UILabel*) [self.tableView viewWithTag:index+yesCountLabelTag1]; // you get your label reference here
UIButton *yesButton=(UIButton *)[self.tableView viewWithTag:index+1+yesVoteButtonTag1];
NSLog(#"Tag IN METHOD: %ld",index+yesVoteButtonTag1);
UILabel *noLabel = (UILabel*) [self.tableView viewWithTag:index+1+noCountLabelTag1]; // you get your label reference here
UIButton *noButton=(UIButton *)[self.tableView viewWithTag:index+noVoteButtonTag1];
These viewWithTag calls are nil when I look at them. The only difference that I can see from my earlier implementation is that the old one had sections and one row, while this one is all rows and one section. So replacing the indexPath.section with indexPath.row should account for that. Also I checked that the tag made in cellForRowAtIndexPath is the same as the row recovered in the yes/no vote method, because it is displaced by one because of the profile cell being created at indexPath.row==0. I tried passing the cell to the yes/no vote method and tried to recover the buttons and labels with contentView as some suggestions made on similar posts. However this didn't seem to solve my problem. Really would appreciate some insight on this.
have you call the '[tableView reload]' method to update the UITableView, it may helps.
Firstly, the table reuse identifier should be used for types of cells, not one for each cell. You have two types, so you should use two fixed reuse identifiers.
ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"ProfileCell"];
if (cell == nil) {
cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"ProfileCell"];
}
and
YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:#"YesNoCell"];
if (cell==nil) {
cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"YesNoCell"];
}
Secondly, rather than trying to get a reference to a cell after creating the table, which isn't working for you, you should initialize the cells completely when they are created. (TableView won't create cells unless they're visible, so you shouldn't rely on their existing at all.)
createProfileCell should really be called initializeProfileCell, because you're not creating the cell in it - you already did that in the line above, or recovered an old one.
Then your call to initializeProfileCell can take a flag specifying whether it is a Yes or No cell and set its properties accordingly.
cell = [self initializeProfileCell:cell isYes:(indexPath.section==0)];
Similarly with createYesNoCell --> initializeYesNoCell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"YOURCELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *title = (UILabel*) [cell viewWithTag:5];
UILabel *vensu =(UILabel*) [cell viewWithTag:7];
vensu.text = #"YOUR TEXT";
title.text = #"YOUR TEXT";
return cell;
}

iPad is unresponsive

I'm currently developing an iOS application. The problem is the iPad 2 I'm using for the testing runs it really slowly. The pushes and pops are slow, the lists are unresponsive, etc.
I tried to isolate one View Controller with only one UITableViewController to find out the reason for the unresponsiveness. The application was still slow.
After that, I commented the webservice call and the parsing call, leaving the application with only one UITableView, showing 30 rows of static text. Still slow.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UILabel *labelTeamA = (UILabel *)[cell viewWithTag:100];
[labelTeamA setText:#"test"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
Other applications already installed run fine.
Please, help me finding out the problem.
Edit:
Just found that the libGoogleAdMobAds.a library was responsible for all the lag in the application. Is there any solution?

Tableview refreshes data every time

First, i have a slide application, there i have a tableview.
By tapping a cell "CellInMainMenu" in the main menu, application opens tableview "TV1" by push, and cells in this tableview("TV1") fill with data by an array. When i slide this tableview("TV1") to side, and tapps again on a cell "CellInMainMenu", application opens again tableview("TV1"), but! Tableview("TV1") starts AGAIN to fill cells with information! How to make application load all data by once at starting application?
(I'm really sorry for my English, because it's not my language)
Can somebody help me?
Code in "TV1":
- (void)viewDidLoad
{
[super viewDidLoad];
daoDS = [[SampleDataDAO alloc] init];
self.ds = daoDS.PopulateDataSource;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TV1CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
SampleData *sample = [self.ds objectAtIndex:indexPath.row];
cell.textLabel.text = sample.HeroesName;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont fontWithName:#"Trajan-Regular" size:18.0f];
return cell;
}
(All appliction works fine, i just want to know how to make just one load but not every time)
The problem is not your table view code, but how you push it. If you invoke a segue, it will always create a new one.
Instead you could only push it the first time, keep a reference to it and then update it based on a selection of your menu. Some custom delegate #protocol is likely the best pattern for this.

UITableView Static Scrolling

I have a simple UITableView using custom UITableViewCells.
The options set on the UITableView's properties are only that the style is set to Grouped.
When I'm trying to scroll down through the different items the scroll is extremely jumpy.
I've researched this quite a bit looking at Tricks for improving iPhone UITableView scrolling performance? and a few other questions on this website. I haven't really been able to find a solution though.
EDIT ****
I use a WSDL webservice to load data into the UITableViewCells.
The cells only have a UITextView and three buttons in it.
EDIT ****
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"NavigatorCell";
NewCell *cell = (NewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.postId = [[items objectAtIndex:indexPath.row] objectForKey:#"PostID"];
cell.post.text = [[items objectAtIndex:indexPath.row] objectForKey:#"Post"];
return cell;
}
I see your NewCell is subclassed.
Don't forget to include this method into your NewCell.m
- (NSString *) reuseIdentifier
{
return #"Cell Identifier";
}
Of course #"Cell Identifier" should be the same that you use in your cellForRowAtIndexPath:.
If you fail to implement this method each cell will be generated from scratch.
Are you using a dequeReusableCellWithIdentifier? Follow the format below. Since you now mention you are loading data from the web you need to do this asynchronously to allow for smooth scrolling. To load from a webservice (asynchronously) there is a nice project just for that here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"yourCellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Setting your tableview to Reuse cells is the most basic way to ensure good performance. Basically it means that instead of creating a new cell for every cell in the tableview, your tableview will recycle the cells that are off screen. The basic setup is below, and more can be learned from the apple documentation on UITableViewDelegate linked here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell Identifier";
CustomCellClassName *cell = (CustomCellClassName *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[CustomCellClassName alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
//Do basic cell construction common to all cells of this type here
//Set background, image etc.
}
//Do specific cell construction here
return cell;
If you're loading data over the network for each cell, you'll see poor performance. Batch your data fetch, then when it's ready tell your tableview to reload itself.
Using Core Data as a temporary backing store, and an NSFetchedResultsController to retrieve the info from Core Data, will save you some work.

DisclosureButton Indicator or Any kind of Accessory Type doesn't seem to appear when set on a UiTableViewCell

This is a weird thing I have been noticing lately. I am not able to see the accessory Type for my UITableView Cell even after setting the accessoryType with the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = #"Move to a New Location";
}else{
cell.textLabel.text = [self.locArray objectAtIndex:indexPath.row];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
It doesn't matter what accessory type I set. It always displays just the text label in the cell but, no button. Am I missing something? Note that this is not the first time I am using a UITableViewCell. I have never come across this problem earlier. Meanwhile I am testing this on iOS6 and have confirmed this behavior both on the iPad simulator as well as the device it self.
Well! I have solved this. My Viewcontroller was initially a subClass of UIViewController instead of UITableViewController. I did this thinking I would be adding extra views to the main View which would have nothing to do with the TableView. I then changed it to UITableViewController and voila it all worked perfect. But then this is a trade off between the flexibility provided by UiViewController and UitableViewController as I now have to add those extra views either in to the headerview or the footerview of the TableView. Nonetheless I have to deal with it any way. I hope this would help some one.

Resources