How can I set identity to each cell in UITableView?
I have a grouped UITable view as custom setting view. Each cell is a view with 3 UITextFields and some other UIViews.
When the cell is created I call a function
[cell initCell:indexPath.section withRow:indexPath.row];
And then the cell knows which configuration to display - this works fine.
My problem is that when the table is scrolled then the cell looses it's identity causing it to save the wrong settings. I saved NSString* view; NSString* row;
but it always have the last value, of the last cell presented (became sidplayed)
Maybe I have a missunderstanding about global parameters, or maybe about the cell reuse. Please Advise, Thanks
My cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section==0)
{
static NSString *CellIdentifierDate = @"SettingsCellPickDate";
SettingsCellPickDate *cell = (SettingsCellPickDate *)[tableView dequeueReusableCellWithIdentifier:CellIdentifierDate];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifierDate owner:self options:nil];
for(id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SettingsCellPickDate class]])
{
cell = (SettingsCellPickDate *) currentObject;
break;
}
}
}
[cell initCell:indexPath.section withRow:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.frame = CGRectZero;
return cell;
}
//tableView.rowHeight = 100;
static NSString *CellIdentifierDate = @"SettingsCellValue";
SettingsCellValue *cell = (SettingsCellValue *)[tableView dequeueReusableCellWithIdentifier:CellIdentifierDate];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifierDate owner:self options:nil];
for(id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SettingsCellValue class]])
{
cell = (SettingsCellValue *) currentObject;
break;
}
}
}
[cell initCell:indexPath.section withRow:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.frame = CGRectZero;
return cell;
}
For example, the SettingsCellValue class looks like this (I change some texts and removed stuff that could suggest my app idea):
#import "SettingsCellValue.h"
@implementation SettingsCellValue
@synthesize defLabel1, defLabel2, defLabel3, defLabel4, text1, text2, text3, typeOfBar;
//@synthesize defLabel, valueField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
}
return self;
}
NSString *view;
NSString *bar;
-(void) initCell:(int)section withRow:(int)row
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger viewType = [defaults integerForKey:@"ScreenType"];
bar = [self getBarAsText:section];
view = [self getViewTypeAsText:viewType];
//Update row 1
defLabel1.text = @"Text1";
text1.text = [defaults objectForKey:[NSString stringWithFormat:@"%@/%@/Caption", view, bar]];
//Update row 2
defLabel2.text = @"Text2";
typeOfBar.selectedSegmentIndex = [defaults integerForKey:[NSString stringWithFormat:@"%@/%@/Type", view, bar]];
}
- (NSString *) getBarAsText:(int)section
{
switch (section) {
开发者_如何学JAVA case 1:
return @"Ba1r";
break;
case 2:
return @"Bar2";
break;
case 3:
return @"Bar3";
break;
default:
return @"Ba1r";
break;
}
}
- (NSString *) getViewTypeAsText:(int)viewType
{
switch (viewType) {
case 1:
return @"viewType1";
break;
case 2:
return @"viewType2";
break;
case 3:
return @"viewType3";
break;
case 4:
return @"viewType4";
break;
default:
return @"viewType1";
break;
}
}
-(IBAction) editingDidEnded:(id) sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (sender == text1) {
[defaults setObject:text1.text forKey:[NSString stringWithFormat:@"%@/%@/Caption", view, bar]];
}
[defaults synchronize];
}
- (void)dealloc {
[ defLabel1, defLabel2, defLabel3, defLabel4, text1, text2, text3, typeOfBar release];
[super dealloc];
}
@end
You shouldn't tie a certain data-object to a certain cell-object, as this would break cell-reuse. While your code doesn't look totally wrong, your question's title points in the direction, that you haven't understood that yet.
One thing I noticed: You use static NSString *CellIdentifierDate
twice. you should rename one.
精彩评论