I am currently making an app which displays the user's profile. For that purpose, I used an UITableViewCell with custom cells for the different types of data (phone numbers, mail addresses, etc...). There is a maximum of 8 cells per profile.
The user is allowed to edit its profile in the simplest way. When the tableview's editing mode is triggered, all editable labels are replaced by textfields. And then turned back to labels when the modifications are finished.
Homever, there seems to be a problem with the cells that are not visible. Everytime they reappear in the view, they are reloaded, the setEditing:YES method is triggered again, etc... Because of this, every change made in the textfield is lost.
Is there a way to prevent the tableview to remove the non-visible cells and to add them back ? There are only eight cells, so it wouldn't be very resources consuming, and I wouldn't have to save their state everytime a change is made.
PS : I have tried several things with the dequeueReusableCellWithIdentifier method and the identifiers of each cell, but I have not managed to achieve what I want. Everytime I hide a cell, its content is refreshed.
In this case, you are not helped with UITableView`s Reusability(Reusability is ofcourse a great thing in most cases) but will have too much difficulty in preserving edits. So you can avoid reusability and prepare your cells before hand.
Add an NSMutableArray
iVar or property in your ViewController
@property (nonatomic, strong) NSMutableArray *cells;
In your viewDidLoad: prepare your cells
without any reuseIdentifier
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Creates tableView cells.
[self createCells];
}
- (void)createCells
{
self.cells = [NSMutableArray array];
TCTimeCell *cellCallTime = [[TCTimeCell alloc] initWithTitle:@"CALL" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeCall];
[_cells addObject:cellCallTime];
TCTimeCell *cellLunchOut = [[TCTimeCell alloc] initWithTitle:@"LUNCH START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchOut];
[_cells addObject:cellLunchOut];
TCTimeCell *cellLunchIn = [[TCTimeCell alloc] initWithTitle:@"LUNCH END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchIn];
[_cells addObject:cellLunchIn];
TCTimeCell *cellSecondMealOut = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealOut];
[_cells addObject:cellSecondMealOut];
TCTimeCell *cellSecondMealIn = [[TCTimeCell alloc] initWithTitle:@"2ND MEAL END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealIn];
[_cells addObject:cellSecondMealIn];
TCTimeCell *cellWrapTime = [[TCTimeCell alloc] initWithTitle:@"WRAP" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeWrap];
[_cells addObject:cellWrapTime];
}
You can populate your tableView from this array.
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.cells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.row];
}
If you have a sectioned tableView, you can prepare your cells as array of arrays
. In that case, your Data Source methods should look like below
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return [self.cells count];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.cells[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.section][indexPath.row];
}