我有一个分组的UITable视图作为自定义设置视图。每个单元格都有一个包含3个UITextFields和一些其他UIViews的视图。
创建单元格时,我调用一个函数
[cell initCell:indexPath.section withRow:indexPath.row];
然后单元格知道要显示的配置 - 这样可以正常工作。
我的问题是,当滚动表格时,单元格会丢失它的标识,导致它保存错误的设置。 我保存了 NSString *视图; NSString * row;
但它始终具有最后一个单元格的最后一个值(变为sidplayed)
也许我对全局参数有疑问,或者可能对细胞重用有误解。 请指教, 感谢
我的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;
}
例如,SettingsCellValue类看起来像这样(我更改了一些文本并删除了可能表明我的app想法的内容):
#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) {
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
答案 0 :(得分:0)
您不应该将某个数据对象绑定到某个单元对象,因为这会破坏单元重用。虽然您的代码看起来并不完全错误,但您的问题标题指向了您尚未理解的方向。
我注意到的一件事:您使用static NSString *CellIdentifierDate
两次。你应该重命名一个。