2015-03-26 3 views
-1

Я использую разные ячейки в своем табло, поэтому я хочу их создать, зависит от чего-то. Но код создания тот же, только имя класса отличается. Как сделать это лучше? Может создать некоторую переменную id classname = (indexPath.section > 3) ? FirstCell : SecondCell;Как создать переменную класса?

if (indexPath.section >3) 
{ 
    FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(FirstCell.class)]; 

    if (!cell) 
     cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:NSStringFromClass(FirstCell.class)]; 
    return cell; 
} 
else 
{ 
    SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(SecondCell.class)]; 

    if (!cell) 
     cell = [[SecondCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:NSStringFromClass(SecondCell.class)]; 
    return cell; 
} 

ответ

0

Просто предложение, я использовал другой apporach в такой ситуации. Я переместил код создания ячейки в соответствующий класс tableviewcell.

@implementation FirstCell 

+ (FirstCell *) cellForTableView:(UITableView *)aTableView 
{ 
    FirstCell *cell = [aTableView dequeueReusableCellWithIdentifier:NSStringFromClass(FirstCell.class)]; 
    if (!cell) 
    { 
     cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(FirstCell.class)]; 
    } 

    return cell; 
} 

@end 

И метод cellForIndexPath будет иметь вид, как в следующем,

if (indexPath.section > 3) 
    return [FirstCell cellForTableView:tableView]; 

return [SecondCell cellForTableView:tableView]; 
+0

Ну, это хорошее решение, но в обоих классах ячеек есть почти такой же код. – Aleksandr

0

ли вы имеете в виду что-то вроде этого?

Class class = (indexPath.section > 3) ? FirstCell.class : SecondCell.class; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(class)]; 
if (!cell) 
    cell = [[class alloc] initWithStyle:UITableViewCellStyleDefault 
         reuseIdentifier:NSStringFromClass(class)]; 
return cell; 
+0

Может быть. Работает ли это для вас? Я пробовал ваш код, получил: Использование незаявленного идентификатора 'cell' – Aleksandr

+0

dequeueReusableCellWithIdentifier не является именем класса, но вы назвали его в раскадровке. – Horst

+0

Скопируйте имя ячейки для повторного использования идентификатора в раскадровке, и вы можете его использовать. – Aleksandr

0

Это работает для вас?

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; 
    _tableView.delegate = self; 
    _tableView.dataSource = self; 
    [_tableView registerClass:[FirstCell class] forCellReuseIdentifier:NSStringFromClass([FirstCell class])]; 
    [_tableView registerClass:[SecondCell class] forCellReuseIdentifier:NSStringFromClass([SecondCell class])]; //ios 6 and above 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *identifier = (indexPath.section > 3) ? NSStringFromClass([FirstCell class]) : NSStringFromClass([FirstCell class]); 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    //cell is guaranteed not to be nil if you register it before 
    return cell; 
}