iOS 6 UITextField inside UITableViewCell with sections get wrong data on scrolling - ios

So basically I am getting the wrong data on every textField by scrolling my TableView (Everything is mixed up). I know, this is because the awesome optimization of the reusable cells in the UITableView, so the indexpath are changing constantly, but still I don't know how to solve this.
Having an UILabel in the same cell I solved this problem by adding a specific tag in every label view, so the tableview knows which data is going to return on a view but for some reason this is not possible for my textfield view.
Here is my code:
#define DESCRIPTION_TAG 10
#define TEXTFIELD_TAG 11
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Description Cell";
// Intatiating my own TableViewCell
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell){
cell = [[CustomLocationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.rowText = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG]; // returning view with unique tag
cell.rowTextField = (UITextField *)[cell viewWithTag:TEXTFIELD_TAG]; // NOT WORKING!!! Data get mix up
cell.rowTextField.delegate = cell; // sending the delegate to custom class cell
cell.delegate = self; // make connection with my #protocol
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
break;
default:
break;
}
return cell;
}
Any help is appreciated.
Here is an update of my data source:
On my CustomTableViewCell I do have outlets for my UILabel and UITextfield
// UITableView DataSource
- (NSArray *)sections
{
if (!_sections){
_sections = [[NSArray alloc] initWithObjects:#"First Description", #"Second Descriptions", nil];
}
return _sections;
}
- (NSMutableArray *)rooms
{
if (!_rooms){
_rooms = [[NSMutableArray alloc] init];
}
return _rooms;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return [self.descriptions count];
break;
case 1:
return [self.rooms count];
break;
default:
break;
}
return 0;
}

I'm not sure exactly what you're trying to accomplish, but here's an example that works with labels and text fields with placeholder text. You'll notice that in cellForRowAtIndexPath, I set both the text and placeholder to nil, so that when a cell is reused, it doesn't have the wrong text in it. Also, when doing the text fields, I check if the array has placeholder text at that index, and if so, I reset the placeholder, and if not I reset the text.
In order for the text field delegate methods to work properly, each text filed needs a unique tag (not just one tag for labels and another for text fields) that I set equal to (indexPath.row + 1000*indexPath.section).
#import "TableController.h"
#import "CustomLocationCell.h"
#interface TableController ()
#property (strong,nonatomic) NSArray *descriptions;
#property (strong,nonatomic) NSMutableArray *descriptionsPlaceholder;
#property (strong,nonatomic) NSArray *rooms;
#property (strong,nonatomic) NSMutableArray *contentPlaceholder;
#end
#implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
self.descriptions = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight"];
self.descriptionsPlaceholder = [#[#"Placeholder1",#"Placeholder2",#"Placeholder3",#"Placeholder4",#"Placeholder5",#"Placeholder6",#"Placeholder7",#"Placeholder8"] mutableCopy];
self.rooms = #[#"Room1", #"Room2", #"Room3", #"Room4",#"Room5", #"Room6", #"Room7", #"Room8"];
self.contentPlaceholder = [#[#"ContentPH1",#"ContentPH2",#"ContentPH3",#"ContentPH4",#"ContentPH5",#"ContentPH6",#"ContentPH7",#"ContentPH8"] mutableCopy];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.descriptions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Description Cell";
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.rowTextField.delegate = self;
cell.rowTextField.tag = indexPath.row + 1000*indexPath.section;
cell.rowTextField.placeholder = nil;
cell.rowTextField.text = nil;
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
if ([self.descriptionsPlaceholder[indexPath.row] hasPrefix:#"Placeholder"]) {
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
if ([self.contentPlaceholder[indexPath.row] hasPrefix:#"ContentPH"]) {
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.contentPlaceholder objectAtIndex:indexPath.row];
}
break;
default:
break;
}
return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag >= 1000 && ![textField.text isEqualToString: #""]) {
[self.contentPlaceholder replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text];
}else if (textField.tag < 1000 && ![textField.text isEqualToString: #""]) {
[self.descriptionsPlaceholder replaceObjectAtIndex:textField.tag withObject:textField.text];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Related

How to give action to tableview row and how to add Add disclosure indicator in it?

Here is description of my class.
At first refer the above image.
So here, I created a button.(see in scene 1 in Image) After clicking a button one table view will appear (see in scene 2 in Image). If I click any cell then that table view cell value display on that button text (see in scene 3 in Image). Here I not given any action to any table view cell.
Now, I given action to these row(only to index 0 for testing). When I click uitableviewcell on Uitableview 1 then it will show another tableview 2 and previous table view hide. see following image.
So in this tableview 2 I want to give action i.e when i click on any row from tableview row of second table for example :Suppose I clicked on Arjun then button label need to change by Arjun and this tableview have to hide or If click on tableview row like Karan then on one pop up I want to display like Karan selected.
In short, I want to know how to give action to that second tableview row and how to add disclosure indicator to first table view.
ViewCotroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet UITableView *tableview1;
#property (weak, nonatomic) IBOutlet UITableView *tableview2;
#property(strong,nonatomic)NSArray *arr1;
#property(strong,nonatomic)NSArray *arr2;
- (IBAction)buttonAction:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arr1=[[NSArray alloc]initWithObjects:#"One",#"Two",#"Three",#"Four",#"Five", nil];
self.arr2=[[NSArray alloc]initWithObjects:#"Arjun",#"Karan",#"Amar", nil];
self.tableview1.delegate=self;
self.tableview1.dataSource=self;
self.tableview2.delegate=self;
self.tableview2.dataSource=self;
self.tableview1.hidden=YES;
self.tableview2.hidden=YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows;
if(tableView == _tableview1) rows = [_arr1 count];
if(tableView == _tableview2) rows = [_arr2 count];
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simple=#"SampleIndentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simple];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simple];
}
// cell.textLabel.text=[self.arr1 objectAtIndex:indexPath.row];
if(tableView == _tableview1)
cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];
[self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
self.tableview1.hidden=YES;
if(tableView == _tableview1)
{
if(indexPath.row==0)
{
self.tableview1.hidden=YES;
self.tableview2.hidden=NO;
if(indexPath.row==0)
{
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
//self.tableview2.hidden=YES;
}
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
- (IBAction)buttonAction:(id)sender {
if(self.tableview1.hidden==YES)
{
self.tableview1.hidden=NO;
}
else
{
self.tableview1.hidden=YES;
}
}
#end
check following answer..!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];
[self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
self.tableview1.hidden=YES;
if(tableView == _tableview1)
{
if(indexPath.row==0)
{
self.tableview1.hidden=YES;
self.tableview2.hidden=NO;
if(indexPath.row==0)
{
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
//self.tableview2.hidden=YES;
}
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}

Trying to add sections with headers to UITableViewController (iOS)

I'm still very new to objective-c, and I've looked all for an answer. I haven't been able to successfully implement anything I've found yet. I'm using an open source sliding menu that I found, and I haven't found anything that fits within the code it uses. (I'd post a link to it, but I can't remember where I found it.)
EDIT: I've got the sections added. Now, I just need to make it so the first cell in the second section only shows up in the first section. http://i.imgur.com/gSIRNJ3.png
I've updated the code below. Here's a reference of what I'm trying to do. Right now, cell 1 shows up in both section 1 and section 2.
SECTION 1: (no header)
- cell 1: "Search Nouns"
SECTION 2: "Noun Conjugation"
- cell 2: "Regular Nouns"
- cell 3: "Irregular Nouns"
- cell 4: "Uncountable Nouns"
#import "BaseTableViewController.h"
#import "KYSlideMenuViewController.h"
#interface BaseTableViewController () {
int _currentCtrNum;
}
#end
#implementation BaseTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 最初に表示するビューを設定する
_currentCtrNum = -1;
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return 1;
else
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section==0)
return 0;
else
return 25;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return #"";
else
return #"reference";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"名詞検索";
break;
case 1:
cell.textLabel.text = #"Side B";
break;
case 2:
cell.textLabel.text = #"Side C";
break;
case 3:
cell.textLabel.text = #"Side D";
default:
break;
}
cell.backgroundColor = [UIColor grayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
for (int i = 0 ; i < [tableView numberOfRowsInSection:0] ; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (i == indexPath.row) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
}
}
// 現在のビューコントローラなら表示するだけ
if (_currentCtrNum == indexPath.row) {
[(KYSlideMenuViewController *)self.parentViewController slideChildTo:KYS_CLOSE];
return;
}
_currentCtrNum = (int)indexPath.row;
// 新しいビューコントローラを生成してコンテナのビューコントローラを入れ替え
UIViewController *toCtr;
switch (indexPath.row) {
case 0: // noun search
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"itemTableView"];
break;
case 1: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"mainView"];
break;
case 2: // navigationなし
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"subView"];
break;
case 3: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"srdView"];
break;
default:
return;
}
[(KYSlideMenuViewController *)self.parentViewController changeTo:toCtr];
}
#end
There also supposed to be a delegate for numberOfSectionsInTableView: where you would return 2;

A uitable view with 3 groups in it

i am a developing a ios app with a view controller and a table view in it. i am trying to load list of items in 3 groups but when i compile it it shows correct count but not showing all the items jus repeating items. please help. let me post my code here.
#interface ViewController ()
#end
#implementation ViewController {
NSArray *menuItems;
NSArray *menuItems2;
NSArray *dash;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
menuItems = #[#"itm1", #"itm2", #"itm3", #"itm4"];
menuItems2 = #[#"itm1", #"itm2", #"itm3", #"itm4"];
dash = #[#"itm1", #"itm2"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [menuItems count];
}
if (section == 1) {
return [menuItems2 count];
}
if (section == 2) {
return [dash count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
#end
Your cellForRowAtIndexPath... method needs to be written so the cell is populated with proper data based on the cell's section and row. Right now you don't do anything but use an empty cell.
You don't configure your cells. After dequeuing a cell, you have to set its title.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Get the correct array, depending on the current section.
NSArray *items = nil;
if (indexPath.section == 0) {
items = menuItems;
}
else if (indexPath.section == 1) {
items = menuItems2;
}
else if (indexPath.section == 2) {
items = dash;
}
// Get the string from the array and set the cell's title
NSString *title = items[indexPath.row];
cell.textLabel.text = title;
return cell;
}

Set an `UIImageView` to not highlighted on a custom UITableViewCell

I'm developing an iOS application with latest SDK.
I have a UITableView with a custom UITableViewCell:
#interface FavouriteCell : UITableViewCell
#property (unsafe_unretained, nonatomic) IBOutlet UIImageView *selectIcon;
#property (unsafe_unretained, nonatomic) IBOutlet UILabel *favName;
#property (nonatomic) BOOL checked;
+ (NSString *)reuseIdentifier;
#end
On selectIcon I set two images from normal and highlighted. When I do cell.checked = !selected; on tableView:didSelectRowAtIndexPath: it works perfectly.
But when I try to reset every selected cell on clearSelectedFavourites:, it doesn't work.
#interface FavViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate, UIAlertViewDelegate>
{
#private
NSMutableArray* _favsSelected;
BOOL* _isOnEditingMode;
}
// ######### FavViewController implementation ##############
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.title = NSLocalizedString(#"Favoritos", #"Favoritos");
self.tabBarItem.image = [UIImage imageNamed:#"fav"];
_favsSelected = [[NSMutableArray alloc] init];
_isOnEditingMode = NO;
}
return self;
}
- (UITableViewCell* )tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath");
static NSString* CellIdentifier = #"FavCell";
FavouriteCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"FavouriteCell" owner:self options:nil];
cell = _favCell;
self.favCell = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.favName.font = [UIFont systemFontOfSize:15.0f];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath: %#", indexPath);
if (tableView.isEditing)
{
BOOL selected = NO;
if ((_favsSelected != nil) && ([_favsSelected count] > 0))
selected = ([_favsSelected indexOfObject:indexPath] != NSNotFound);
FavouriteCell* cell =
(FavouriteCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(cell.checked ? #"Yes" : #"No");
cell.checked = !selected;
if (selected)
[_favsSelected removeObject:indexPath];
else
[_favsSelected addObject:indexPath];
}
}
- (IBAction)editFavList:(id)sender
{
NSLog(#"edit button clicked!");
if ([_favList isEditing])
{
[self clearSelectedFavourites];
[_favList setEditing:NO animated:YES];
[_editButton setImage:[UIImage imageNamed:#"ButtonEdit.png"] forState:UIControlStateNormal];
}
else
{
[_favList setEditing:YES animated:YES];
[_editButton setImage:[UIImage imageNamed:#"done_button.png"] forState:UIControlStateNormal];
}
}
- (void)clearSelectedFavourites
{
for(int index = 0; index < [_favsSelected count]; index++)
{
NSIndexPath* indexPath = (NSIndexPath*)[_favsSelected objectAtIndex:index];
FavouriteCell* cell = (FavouriteCell*)[self tableView:_favList cellForRowAtIndexPath:indexPath];
cell.checked = NO;
}
[_favsSelected removeAllObjects];
}
- (void)configureCell:(FavouriteCell *)cell
atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = nil;
object = [self.favListFetchedResultsController objectAtIndexPath:indexPath];
cell.favName.text = [[object valueForKey:#"name"] description];
}
Do you know why?
I have tried to set selectIcon to not hightlighted manually, but it doesn't work, and that code if the latest test I did.
I have also tested this: [cell setHighlighted:NO]; on clearSelectedFavourites: and it doesn't work.
You are not showing us your custom [self configureCell:cell atIndexPath:indexPath]; function, so I cannot know what's going on.
However a few things first:
The cells on the TableView are being reused.
So you cell select state needs to be set every time for each cell.
Also on your clearSelectedFavourites function, you are getting a reference to all selected cells, then clear their selection, which is pointless, because the cells are going to be reused.
You only need to loop through the visible UITableView cells and change their selection status, then clear your _favsSelected array...
- (void)clearSelectedFavourites
{
NSArray *cells = [self.tableView visibleCells];
for (FavouriteCell *cell in cells)
{
cell.checked = NO;
//might not be needed, or might be needed
[cell setNeedsDisplay];
}
[_favsSelected removeAllObjects];
}
Finally on your configureCell function, you should check if the cell should be checked or not, based on your _favsSelected array entries and just select it or not.
Edit
In your configureCell function you should check if the cell needs to be checked or not:
- (void)configureCell:(FavouriteCell *)cell
atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = nil;
object = [self.favListFetchedResultsController objectAtIndexPath:indexPath];
cell.favName.text = [[object valueForKey:#"name"] description];
cell.checked = [_favsSelected containsObject:indexPath];
}
You can do like this :
[tbl beginUpdates];
/* call you method with animation */
[tbl endUpdates];
[tbl reloadData];

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