tech_human
tech_human

Reputation: 7110

Alternative for cellForRowAtIndexPath

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?

Upvotes: 0

Views: 408

Answers (2)

GregP
GregP

Reputation: 1624

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

Upvotes: 0

Caleb
Caleb

Reputation: 125007

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.

Upvotes: 6

Related Questions