0

I am looking for an alternative method for cellForRowAtIndexPath. What I am trying to do is in my private method, I am checking if the text on the exiting cell is present into an array, if it is then select the cell else remain it deselected. I learnt that cellForRowAtIndexPath works only on visible cells and returns nil if the cell is invisible. Below is my present code:

- (void)selectCells:(NSArray *)array
{
    for (int row = 0; row < [self.tableView numberOfRowsInSection:1]; row ++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row 1];

        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        if ([array containsObject:cell.textLabel.text] && !cell.isSelected)
        {
           [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

What else can I use to achieve this?

Paige DePol
  • 1,121
  • 1
  • 9
  • 23
tech_human
  • 6,592
  • 16
  • 65
  • 107
  • Be clear: Do you want to replace cellForRowAtIndexPath in the UITableView, or cellForRowAtIndexPath in the UITableViewDelegate? They are two entirely different things. (And you can't really "replace" either one -- they must exist and be called.) – Hot Licks Jan 29 '14 at 21:15
  • I don't want to replace the actual cellForRowAtIndexPath. I am looking for an alternative to get all the cells on my tableview. So in the above method I am using cellForRowAtIndexPath to get cells, but I understand it won't give me the cells which are not visible. I want something in the above method that could return me all the cells or suggestion for some best way to achieve what I want. – tech_human Jan 29 '14 at 21:18
  • Well, you didn't answer my question. But you're asking the wrong one. You don't try to maintain all the cells, simply the data IN the cells. I find an NSMutableArray containing NSMutableDictionarys works well for this -- each cell attribute is a name/value pair in the corresponding dictionary. – Hot Licks Jan 29 '14 at 21:26

2 Answers2

6

I am checking if the text on the exiting cell is present into an array

That sounds like a fundamental problem. In the Model View Controller paradigm, the view is responsible for displaying data and interacting with the user, while the model is responsible for storing and operating on data. You shouldn't be looking at the content of a cell and making decisions based on that; you should instead look at the data in your model.

UITableView works the way it does because if you're following MVC, it's completely unnecessary to keep cells around that aren't visible. Whenever the table needs to display a new cell, it'll ask you for that cell by calling -tableView:cellForRowAtIndexPath:.

I am looking for an alternative to get all the cells on my tableview.

The cells don't all exist at the same time. You could conceivably call your data source's -tableView:cellForRowAtIndexPath: method with different index paths, just as the table does, and that would return each cell. But that's probably not the right solution; you need to know where the data for the table is stored in order to populate the table's cells in the first place, so you should instead just look at the data directly.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

When you populate the cells store them all with unique identifiers in an array. If you don't have too many cells this should work. Then you can just use self.cellArray or whatever you call it and get the object at the index path. I would keep the data separate though as suggested and then when you render the cell in cellForIndexPath make sure that you select or deselect the cell according to your data.

Setting static table cells

Community
  • 1
  • 1
GregP
  • 1,584
  • 17
  • 16