У меня есть сгруппированное представление UITable как пользовательский параметр. Каждая ячейка представляет собой представление с 3 UITextFields и некоторыми другими UIView.Как установить идентификатор каждой ячейки в UITableView?
При создании ячейки вызвать функцию
[cell initCell:indexPath.section withRow:indexPath.row];
И тогда клетка знает, какая конфигурация для отображения - это работает отлично.
Моя проблема заключается в том, что когда таблица прокручивается, ячейка теряет свою личность, заставляя ее сохранять неправильные настройки. Я сохранил NSString * view; NSString * row;
но всегда есть последнее значение, последней ячейки, представленной (стал sidplayed)
Может быть, у меня есть missunderstanding о глобальных параметров, или, может быть, о повторном использовании клеток. Пожалуйста, посоветуйте, Спасибо
Мой 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 выглядит следующим образом (я изменить некоторые тексты и удалены вещи, которые могли бы предложить мое приложение идею):
#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
Было бы полезно, если бы ур 'tableView: cellForRowAtIndexPath:' – vikingosegundo
Вам лучше всего было бы обслуживать, просматривая MVC и просматривая примерный код набора tableview. Вы делаете так неправильно, что вам нужно начать все сначала и изменить то, как вы думаете. Это больше, чем я хочу помочь вам с собой. Пожалуйста, узнайте, что такое MVC, для чего он предназначен, и что именно так разрабатываются и планируются каркасы. Кроме того, узнайте о повторном использовании ячеек, о том, как работают таблицы, и т. Д. В этом замечании есть несколько примеров в ссылке i. Удачи. – jer
Что такое initCell: withRow: похоже? –