iOS Segue opening two view controllers (assuming two table cells clicked) - ios

When I click a table cell in my application it opens two view controllers. Which it seems to assume that I have clicked two cells, since it opens the cell before and the one I have clicked.
The first 3 cells are pretty much static, every other cell underneath the "ingredients" heading are dynamic. Hence why indexPath - 3
I created the segue between the table view and the view controller in the interface builder.
Table View:
First view controller/segue:
Second view controller segue:
Code for the segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedIngredient = self.selectedProduct.ingredientsArray[indexPath.row - 3];
[self performSegueWithIdentifier:#"ingViewSegue" sender:self];
NSLog(#"selected ingredient %#", self.selectedIngredient);
}
#pragma mark segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
IngredientWebViewController *webView = segue.destinationViewController;
webView.selectedIngredient = self.selectedIngredient;
}
Number of rows in table:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_selectedProduct.ingredientsArray count] + 3;
}
Code to populate table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
TitleCell* titleCell = (TitleCell*)[self.tableView dequeueReusableCellWithIdentifier:#"TitleCell" forIndexPath:indexPath];
titleCell.nameLabel.text = self.selectedProduct.productName;
titleCell.brandLabel.text = self.selectedProduct.productBrand;
return titleCell;
}
else if(indexPath.row == 1)
{
return [self.tableView dequeueReusableCellWithIdentifier:#"RatingCell" forIndexPath:indexPath];
}
else if(indexPath.row == 2)
{
return [self.tableView dequeueReusableCellWithIdentifier:#"IngredientHeaderCell" forIndexPath:indexPath];
}
else
{
IngredientsCell* ingCell = (IngredientsCell*)[self.tableView dequeueReusableCellWithIdentifier:#"IngredientCell" forIndexPath:indexPath];
ingCell.ingredientNameLabel.text = self.selectedProduct.ingredientsArray[indexPath.row - 3];
if([self.selectedProduct.ratingsArray[indexPath.row -3] isEqual: #"4"])
{
ingCell.ingredientsImage.image = [UIImage imageNamed:#"icon_safe.png"];
}
else if([self.selectedProduct.ratingsArray[indexPath.row -3] isEqual: #"3"])
{
ingCell.ingredientsImage.image = [UIImage imageNamed:#"icon_low.png"];
}
else if([self.selectedProduct.ratingsArray[indexPath.row -3] isEqual: #"2"])
{
ingCell.ingredientsImage.image = [UIImage imageNamed:#"icon_med.png"];
}
else if([self.selectedProduct.ratingsArray[indexPath.row -3] isEqual: #"1"])
{
ingCell.ingredientsImage.image = [UIImage imageNamed:#"icon_high.png"];
}
else
{
ingCell.ingredientsImage.image = [UIImage imageNamed:#"icon_safe.png"];
}
return ingCell;
}
}
Code to alter cell height:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"TitleCell"];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
else if(indexPath.row == 1)
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"RatingCell"];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return 118;
}
else if(indexPath.row == 2)
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"IngredientHeaderCell"];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1;
}
else
{
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"IngredientCell"];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return 60;
}
}

A segue will be performed automatically while a cell is tapped. Try the following code:
// - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// {
// self.selectedIngredient = self.selectedProduct.ingredientsArray[indexPath.row - 3];
// [self performSegueWithIdentifier:#"ingViewSegue" sender:self];
// NSLog(#"selected ingredient %#", self.selectedIngredient);
// }
#pragma mark segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
IngredientWebViewController *webView = segue.destinationViewController;
webView.selectedIngredient = self.selectedProduct.ingredientsArray[indexPath.row - 3];
}

Related

Attempting to add an inline UIDatePicker to a UITableViewCell

I'm attempting to create an inline DatePicker inside a TableView cell, similar to this and this SO thread.
I create the date picker using the method below, which is called when the view is loaded:
- (void)createDatePicker{
_datePicker = [[UIDatePicker alloc]init];
_datePicker.datePickerMode = UIDatePickerModeTime;
_datePicker.clipsToBounds = YES;
_datePicker.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
NSLog(#"date picker created");
}
I then check to see if the bottom row of the third section in my table view is selected. If it is, and the date picker already isn't showing, then I call the method to show the date picker's cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == HourTimeZoneRow && self.datePickerIsShowing == NO)
{
NSLog(#"Time of day section");
[self showDatePickerCell];
} else
{
[self hideDatePickerCell];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Below are the methods to show the date picker's cell:
- (void)showDatePickerCell {
[self.tableView addSubview:_datePicker];
self.datePickerIsShowing = YES;
self.datePicker.hidden = NO;
[self.tableView reloadData];
NSLog(#"Show date picker");
}
...and hide the cell:
- (void)hideDatePickerCell {
self.datePickerIsShowing = NO;
self.datePicker.hidden = YES;
[self.tableView reloadData];
NSLog(#"Hide date picker");
}
Below is the method to determine if the table view needs to add an extra cell to display the UIDatePicker.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case TableSectionOne:
return TotalRowsSection1;
break;
case TableSectionTwo:
return TotalRedZoneRows;
break;
case TableSectionThree:
if (self.datePickerIsShowing == YES) {
return TableSectionThree + 1;
}
else {
return TableSectionThree;
}
break;
default:
return 0;
break;
}
}
Finally, the method below is what is supposed to determine the height of the date picker's cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = self.tableView.rowHeight;
if (indexPath.row == HourTimeZoneRow){
rowHeight = 164;
}
else {
rowHeight = self.tableView.rowHeight;
}
return rowHeight;
}
However, what's happening is that when the last row of the third section is selected, the date picker is displayed in the first row of the first section. Does anyone have any suggestions on what I'm doing wrong?
You're adding the datePicker to the tableView, not to a cell within the tableView.
Remove this line from showDatePickerCell:
[self.tableView addSubview:_datePicker];
Then add it (or unhide it) within the proper cell within cellForItemAtIndexPath.
(something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.row == HourTimeZoneRow) {
// show the datePicker here
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
)

indexPathForSelectedRow always returns 0 for both selected section and row

I am developing iOS App with UITableViewCell.
When a UITableViewCell is tapped and goes to another screen with segue, indexPathForSelectedRow always returns 0 for both selected section and row.
My code is following.
Could you tell me how to solve this problem?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0) {
return 3;
}
else if(section == 1) {
return 2;
}
return 0;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifer = #"editCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
if(cell==nil){
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
}
UITextView *editText = (UITextView *)[cell viewWithTag:1];
if(indexPath.section == 0) {
if(indexPath.row == 0) {
editText.text = #"aaa";
} else if(indexPath.row == 1){
editText.text = #"bbb";
} else {
editText.text = #"ccc";
}
}
else if(indexPath.section == 1) {
if(indexPath.row == 0) {
editText.text = #"ddd";
} else if(indexPath.row == 1){
editText.text = #"eee";
}
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"editDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(#"a%d",indexPath.section);
NSLog(#"b%d",indexPath.row);
}
Looking at your code, you're missing ;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
perform a transition from this method and you'll have access to the indexPath directly
You probably deselected your row somewhere in your code. Most likely in didSelectRowAtIndexPath method. It's a common practice to deselect a row that has just been selected. If you deselected the row, this will cause you to get 0 in the prepareForSegue method when you try to get the indexPath.row.

displaysSearchBarInNavigationBar randomly triggers searchBar?

So I was trying to combine the search bar with the Nav bar for tableview in iOS7.
I called
self.searchDisplayController.displaysSearchBarInNavigationBar = YES; And it looks fine.
However, when I tap anywhere in the tableview, the search bar is activated. There's essentially no way I can regularly click a table cell.
I'm wondering what exactly did I miss here?
And for reference the code related to tableview looks like this: (I used storyboard to handle click logic)
#pragma mark - Table view delegate method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.contacts[section] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"contactCell";
UITableViewCell *cell;
Contact *contact;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
contact = searchResults[indexPath.row];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:7];
UIImageView *starMark = (UIImageView *)[cell viewWithTag:9];
NSString *nameString = [NSString stringWithFormat:#"%# %#",contact.firstName, contact.lastName];
if ([contact.star isEqual:#0]) {
starMark.hidden = YES;
} else {
starMark.hidden = NO;
}
[nameLabel setText:nameString];
return cell;
}
And the segue method is defined here:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"pushContactDetail"]) {
NSIndexPath *indexPath;
Contact *contact;
if (self.searchDisplayController.active == YES) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
contact = searchResults[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//get indexPath from selected sushi
//initialize the detail view controller and push it
CIContactViewController *destViewController = segue.destinationViewController;
destViewController.contact = contact;
}
}

How to toggle UITableView Cell selected status

I have a UITableView with a custom Cell, the cell contains a UIImageView and a UILabel. Now When I load my table first time, It loads with a same image on each cell and different labels, which it takes from the LabelArray.
Now the image I am talking about is a radioButton, So when the user clicks the cell, the image changes. If user clicks again it changes to default state.
For this to happen, I have used this function and also I have declared a bool variable in my customcell class called selectionStatus.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0)
{
if(cell.selectionStatus == TRUE)
{
//Do your stuff
cell.selectionStatus = FALSE;
}
else
{
//Do your stuff
cell.selectionStatus = TRUE;
}
}
if(indexPath.row == 1)
{
if(cell.selectionStatus == TRUE)
{
//Do your stuff
cell.selectionStatus = FALSE;
}
else
{
//Do your stuff
cell.selectionStatus = TRUE;
}
}
}
This works fine, (but I want to know whether it is a proper way, or can we check the cell.selected property) and I am able to get that effect. But now when I close the View and open it again the function
Edit based on below comments with #Anil
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.checkedIndexpath count] == 0)
{
[tableCell.selectionImage setImage:#"xyz.png"];
}
else
{
for (int i =0; i <[self.checkedIndexPath count]; i++)
{
NSIndexPath *path = [self.checkedIndexPath objectAtIndex:i];
if ([path isEqual:indexPath])
{
[tableCell.selectionImage setImage:#"abc.png"]
}
else
{
[tableCell.selectionImage setImage:#"xyz.png"]
}
}
return tableCell;
Regards
Ranjit
Another option for toggling UItableViewCells...
In the willSelectRowAtIndexPath return nil for NSIndexPath if cell is already selected, also set your _selectedIndexPath instance variable.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView.delegate tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
indexPath = nil;
}
_selectedIndexPath = indexPath;
return indexPath;
}
In didSelectRowAtIndexPath and didDeselectRowAtIndexPath update your cell based on the property cell.selected ...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
// do stuff
} else {
// do stuff
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.selected) {
// do stuff
} else {
// do stuff
};
}
and finally in viewDidLoad set the clearsSelectionOnViewWillAppear property...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
you have to save the index path of the selected row in didSelectRowAtIndexPath and check the index path in cellForRowAtIndexPath: set the corresponding image
Do you want multiple selection? try this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0)
{
if(cell.selectionStatus == YES)
{
[self.checkedIndexPaths addObject:indexPath];
//Do your stuff
cell.selectionStatus = NO;
}
else
{
[self.checkedIndexPaths removeObject:indexPath];
//Do your stuff
cell.selectionStatus = YES;
}
}
}
Edit
In cellForIndexPath check like this
// Set the default image for the cell. imageXYZ
for (NSIndexPath *path in self.checkedIndexPath)
{
if ([path isEqual:indexPath])
{
//set the changed image for the cell. imageABC
}
// no need of else part
}
Do the exact we will see
Take a boolean in your model class and update it on didSelectRow method and reload table. In cellForRowAtIndexPath check this like that.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
After making your cell check like that
ModelClass *yourModel=[self.yourArray objectAtIndex:indexPath.row];
if(yourModel.selection)
[cell.customImage setImage:[UIImage imageName:#"selected"];
else
[cell.customImage setImage:[UIImage imageName:#"un_selected"];
}
Check this link
https://github.com/adnanAhmad786/Quiz-View--TableView
First of all for toggling purpose declare a bool value selectionStatus in tableview cell class like this:
#property(nonatomic)BOOL selectionStatus;
Declare a NSMutableArray to store the selected indexpaths (for multi-selection purpose)
self.checkedIndexPaths = [[NSMutableArray alloc] init];
In didSelectRowAtIndexPath
a. if Bool value selectionStatus is No Insert indexPath to NSMutableArray checkedIndexPaths and set cell.selectionStatus = YES
b. if Bool value selectionStatus is Yes Remove indexPath from NSMutableArray checkedIndexPaths and set cell.selectionStatus = NO
c. Reload tableView each time
Find the code below :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tableCell = [self.tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStatus == YES)
{
[self.checkedIndexPaths removeObject: indexPath];
cell.selectionStatus = NO;
}
else
{
[self.checkedIndexPaths addObject: indexPath];
cell.selectionStatus = YES;
}
[self.tablee reloadData];
}
In cellForRowAtIndexPath
a. First set cell image to unchecked i.e xyz.png here
[tableCell.selectionImage setImage:[UIImage imageNamed:#"xyz.png"]];
b. Set image as checked for all index paths in NSMutable array self.checkedIndexPaths
for (NSIndexPath *path in self.checkedIndexPaths)
{
if ([path isEqual:indexPath])
{
[tableCell.selectionImage setImage:[UIImage imageNamed:#"abc.png"]];
}
}

values change when scrolling my tableview

my tableview changes values when I scroll up or down. It seems to use the same values from other parts of the table. I imagine its an error in my cell creation, here is the code.
Please tell me what I did wrong, thank you!
EDITED ADDED WHOLE CODE
//global indexpath to remember which cell tapped
NSIndexPath *globalPath;
#interface SearchViewController ()
#end
#implementation SearchViewController
//Load implementation once per launch
- (void)viewDidLoad
{
[super viewDidLoad];
[self linkInputTableToDelegate];
_temporaryResultsArray =[[NSMutableArray alloc]init];
_flurryArray=[[NSMutableArray alloc]init];
_numberOfSections=6;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:NO];
[InputTable reloadData];
textFromUserDefaults=[[[HelperMethods alloc]init]getObjectUserDefault:#"textFiltered"];
[self addTextToFlurryArrayForFlurryAndSavedLists:_textFromUserDefaults];
}
-(void)viewDidDisappear:(BOOL)animated{
}
- (IBAction)searchButtonPressed:(UIButton *)sender {
self.tabBarController.selectedIndex = 1;
}
//Makes the input table respond to delegate table view methods
-(void)linkInputTableToDelegate{
_inputTable.dataSource=self;
_inputTable.delegate=self;
}
-(void)performSearch:(NSString*)text{
//do search
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int numberOfRows=_numberOfSections;
//Rows for iPhone 4
if ([[UIScreen mainScreen]bounds].size.height==480) {
numberOfRows=numberOfRows;
//Rows for iPhone 5
}else if ([[UIScreen mainScreen]bounds].size.height==568){
numberOfRows=numberOfRows+1;
}
return numberOfRows;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//In reality groups are created with 1 row inside, this is to allow spacing between the rows
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
}
//Is the cell the same as the one clicked when going to ingredient filter
BOOL cellIndexPathSameAsSelected=[self isCellIndexSameAsPreviousClicked:indexPath];
cell.textLabel.textColor=[UIColor blackColor];
if (cellIndexPathSameAsSelected && _textFromUserDefaults!=nil) {
if (![cell.textLabel.text isEqualToString:_textFromUserDefaults]) {
cell.textLabel.text=_textFromUserDefaults;
[self performTextSearch:_textFromUserDefaults];
}
}
return cell;
}
//Compares the previous clicked cell with the cell now selected
-(BOOL)isCellIndexSameAsPreviousClicked: (NSIndexPath*)cellPath{
if (cellPath.row == globalPath.row && globalPath.section==cellPath.section) {
return YES;
}
else{
return NO;
}
}
- (void)updateTableViewWithExtraRow :(NSIndexPath*)rowSelected{
NSLog(#"number of sections =%i",_numberOfSections);
if (rowSelected.section == _numberOfSections) {
_numberOfSections ++;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[[[HelperMethods alloc]init]saveObjectToUserDefaults:cellText :#"textFiltered"];
globalPath = indexPath;
[self updateTableViewWithExtraRow:indexPath];
}
-(void)addTextToFlurryArrayForFlurryAndSavedLists:(NSString*)text{
if ([_flurryArray count]==0 &&[text length]>0) {
[_flurryArray addObject:text];
}
for (int i=0;i<[_flurryArray count];i++) {
NSString *textInArray=[_flurryArray objectAtIndex:i];
if (![textInArray isEqualToString:text]) {
[_flurryArray addObject:text];
}
}
NSLog(#"Total number of saved items = %i",[_flurryArray count]);
}
// Dispose of any resources that can be recreated.
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
The problem is you are only changing the texts in the cells, when those conditions are met. For example, when your cellIndexPathSameAsSelected is NO, you leave the cell unmodified. So you should add an else and do some settings there too.
EDIT:
if (cellIndexPathSameAsSelected && _textFromUserDefaults!=nil) {
if (![cell.textLabel.text isEqualToString:_textFromUserDefaults]) {
cell.textLabel.text=_textFromUserDefaults;
[self performTextSearch:_textFromUserDefaults];
}
} else {
cell.textLabel.text = [NSString string];
}
After your [tableView dequeueReusableCellWithIdentifier:kCellID]; call, you have to check if your cell can actually be reusable or not with the followings statements :
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:...];
}
You should be doing something like this
static NSString *kCellID = #"Cell";
// Acquire the cell if possible.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) // Not possible to re-use cell, so create a new cell
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
}

Resources