Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:03:56+00:00 2026-06-16T19:03:56+00:00

The code below is creating a search for many strings. Initially there are 5

  • 0

The code below is creating a search for many strings. Initially there are 5 rows, when you reach row five, it adds another row. Instead of just directly editing the row, i load a filter controller (another view controller that as you type it completes words for you). When the user finishes finding a word he clicks it and comes back to this view controller. Now i want to fill the cell that was originally tapped with the text from the filter.

I tried asking earlier and didn’t get any concrete answers.

I am running into a problem where when i scroll (after adding a new row), it starts filling in those rows with info already in the table, (as opposed to staying blank)

Please help me where i am going wrong

//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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:03:57+00:00Added an answer on June 16, 2026 at 7:03 pm

    I have a couple of reactions looking at the code:

    1. A couple of observations about the proper use of the UITableViewDataSource methods, specifically numberOfRowsInSection, numberOfSectionsInTableView, and cellForRowAtIndexPath:

      • These really should be driven by some model data structure (e.g. a NSMutableArray) and nothing else;

      • These methods should be stateless. They should not relying on the value of some NSString instance variable, like _textFromUserDefaults) but rather always look up the value in the NSMutableArray model structure on the basis of the value of the indexPath parameter. You simply cannot make any assumptions about when cellForRowAtIndexPath will be called. This may well account for your duplicate values.

      • None of these should be doing anything besides responding to the UITableView inquiry. For example, your cellForRowAtIndexPath is invoking performTextSearch. It really shouldn’t do anything except return the cell.

      • Your cellForRowAtIndexPath currently has conditional logic and only updates the cell if certain conditions holds. Because cells are reused, you really want to make sure that you initialize the cells regardless. You can’t be assured that the cell is blank when you get it, nor that the previous contents are the previous values for that indexPath. Because cells are reused, it could be for an entirely different row. This could also account for your duplicative entries.

    2. Regarding the interaction of the master view controller and the details view controller, there are more elegant ways than passing data back and forth via NSUserDefaults. For example when you initiate the details view controller, you could just pass it the information it needs. And when it’s done, it should call a method in the master view controller to update the data in the master view. To do that, the master view controller should conform to some protocol of your own creation. If you see the example I shared via chat, you can see what that might look like. Anyway, by having some delegate method in the master view controller that the detail view controller calls when it’s done, that eliminates the rather fragile technique of using viewDidAppear to control the updating of the master table view.

    3. You might want to contemplate employing “edit” (which allows you to delete, possibly also edit a particular row) and “add” buttons like the standard “master-detail” template that Xcode provides. There are a number of standard conventions here that might be better than having an array of blank cells that you can then tap on. Clearly, your user experience is entirely up to you, but you can always contemplate whether there are existing, familiar conventions that you might employ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below is creating a server to communicate with clients.This code works fine
In the code below is the initial first loop needed when creating a 2D
I am creating a dropdown contact form and you can view the code below:
I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I am creating a random ID using the below code: from random import *
I am creating a chunk of HTML/JavaScript with the below code: $result = mysql_query(SELECT
I'm having some trouble with looping and creating MS Excel docs, code snippet below
I am creating a form in CakePHP.A snippet of the code is given below.
I have the code below. The line string content = twitterMsg.text; is creating the
I have this code below which creating a background radial circle gradient. it's working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.