Reputation: 19303
I have an array called called totalWins
, inside it there are arrays called winningNumbers
. Inside it there are 70 numbers I need to display in a UITableView cell.
I've subclassed UITaableViewCell and created some UITextField
outlets.
The good old fashion way doing this:
NSArray *winningNumbers = [[_currentSearchResults objectAtIndex:indexPath.row]objectForKey:@"winningNumbers"];
cell.txtFld1.text = [NSString stringWithFormat:@"%@",[winningNumbers objectAtIndex:0]];
cell.txtFld2.text = [NSString stringWithFormat:@"%@",[winningNumbers objectAtIndex:1]];
....
....
Works great. I've tried to save up some coding time and did this: (after seting up the textfields tags of course)
for(int i=0;i<70;i++)
{
UITextField *tempField = (UITextField *) [self.view viewWithTag: 777+i];
tempField.text = [NSString stringWithFormat:@"%@",[winningNumbers objectAtIndex:i]];
}
The problem is that because of the reuse of the cells the numbers are messed up when scrolling the table view. What's the preferred way to accomplish the above without writing cell.txtFld1.text = [NSString stringWithFormat:@"%@",[winningNumbers objectAtIndex:0]];
70 times?
Upvotes: 0
Views: 110
Reputation: 650
I haven't tried this but it may work:
for (id subview in [cell.contentView subviews]) {
if([subview isKindOfClass:[UITextField class]]) {
UITextField *textfield = (UITextField*)subview;
textfield.text = [NSString stringWithFormat:@"%@",[winningNumbers objectAtIndex:(textfield.tag-777)]];
}
}
Hope this helps.
Upvotes: 1
Reputation: 4817
Just remove all created textfields and create them each time.
for(UIView *view in cell.contentView.subviews)
{
[view removeFromSuperView];
}
Upvotes: 0
Reputation: 7850
Subclass UITableViewCell and make an array of UITextFields.
@interface YourCell: UITableViewCell
@property (nonatomic, readonly) NSArray *textfields;
@end
Store references to your text fields in ivar backing up textfields
property.
Then you can do
NSArray *textfields = cell.textfields;
for (int i = 0; i < textfields.count; i++)
{
UITextfield *field = [textfields objectAtIndex:i];
// set textfield's text
}
Upvotes: 0
Reputation: 4485
Use [cell.contentView viewWithTag: 777+i] instead self.view. Your's solution is ok.
Upvotes: 1