0

I have a UITableView called UserProductViewController and this table view used for two purpose "create and edit" some data. When user click "edit button" open this UserProductViewController edit mode and populate some data within cell from another method. I don't want to store all server-side values in property instead I was use Lazy instantiation cell. This approach sometimes makes trouble. To be clear here is my code this approach correct or not ? Could you please guide me correct way to do this?

Implementation Interface

@interface UserProductViewController() 
@property(strong, nonatomic) MyCustomTableViewCell *myCustomCell;
@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...

    if(indexPath.row == 2) return self.myCustomCell;

    ...
}

- (MyCustomTableViewCell *)myCustomCell {
    if(!_myCustomCell) {
        _myCustomCell = [self.tabView dequeueReusableCellWithIdentifier:@"CustomIdentifier" forIndexPath:[self customIndexPath]];
    }

    return _myCustomCell;
}

- (NSIndexPath *)customIndexPath {
    // 2 correspond to cellForRowAtIndexPath indexPath value.   
    return [NSIndexPath indexPathForRow:2 inSection:0];
}

Server side method I'm using that cell as a property like this:

- (void) getUserValues {

        ....
        // getting server-side result
       //  completion block
      self.myCustomCell.titleLabel = result.title;
}

Other question is what would happen if I don't want to use tableview dequeue system ?

serhat sezer
  • 1,330
  • 1
  • 10
  • 26
  • `return myCustomCell` seems like it would generate a static analyzer error because it should either be `_myCustomCell` or `self.myCustomCell`. That said, what reason is there to save the cellview in this VC? You just want a convenient pointer to that cell? – stevesliva Mar 23 '15 at 22:43
  • @stevesliva it's self.myCustomCell sorry. – serhat sezer Mar 23 '15 at 22:50
  • @stevesliva I didn't understand your question. But I just want to add some values to cell like a property. So, another method I called cell like this self.myCustomCell.titleLabel.text = .... etc. – serhat sezer Mar 23 '15 at 22:52
  • It's concerning that you don't access the view through the view hierarchy mostly because of how UITableView manages cellviews behind the scenes. Saving the cellview in a strong property at one point in time doesn't ensure it's always the cellview you want at indexPath 0,2. Calling `cellForRowAtIndexPath` is really what you ought to do. Even if you were to save it in a weak property, I don't think nil-checking it necessarily guarantees it's still the cell displayed at indexPath 0,2. `cellForRowAtIndexPath` and a typecast might be longwinded, but they're not heavyweight. – stevesliva Mar 24 '15 at 00:38
  • @stevesliva aww still doesn't work! – serhat sezer Mar 24 '15 at 09:29
  • Is there another suggestion? – serhat sezer Mar 24 '15 at 12:22

0 Answers0