2013-04-15 4 views
0

Я прекрасно понимаю, что здесь много проблем, хотя я в тупике и не могу решить это самостоятельно. Я только хочу, чтобы отобразить 4 клетки всего в моем myTableView2UITableView, но я нахожу, что мои клетки повторного использования нет сомнений из-за этой линииПроблема повторения с ячейками UITableView

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

Я понимаю, если я удалить if(!cell) линию я мог бы отключить UITableView от создания новых ячеек, но это не работает, потому что оно требует создания ячейки. Есть предположения? Я разместил свой код ниже.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (!cell) 
{ 
    NSLog(@"CREATING NEW CELL"); 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.contentView.autoresizesSubviews = YES; 
    cell.contentView.clipsToBounds = YES; 
} 
if (tableView == self.myTableView) 
{ 
    if ([[array objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]]) 
    {   
    cell.textLabel.text = [[array objectAtIndex:indexPath.row] stringValue]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    else 
    {   
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
} 
if (tableView == self.myTableView2) 
{ 
    self.myTableView2.opaque = NO; 
    self.myTableView2.backgroundColor = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(102.0/255.0) alpha:1.0]; 
    self.myTableView2.backgroundView = nil; 

    if ([[array2 objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]]) 
    { 
     cell.textLabel.text = [[array2 objectAtIndex:indexPath.row] stringValue]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.numberOfLines = 0; 
     [cell.textLabel sizeToFit]; 
     cell.textLabel.font = [self fontForCell]; 
    } 
    else 
    { 
     cell.textLabel.text = [array2 objectAtIndex:indexPath.row]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 
     cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.numberOfLines = 0; 
     [cell.textLabel sizeToFit]; 
     cell.textLabel.font = [self fontForCell]; 
    } 
} 
return cell; 
} 

другие соответствующие данные:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
if (tableView==self.myTableView) 
{ 
    return [array count]; 
} 
else if(tableView==self.myTableView2) 
{ 
    return [array2 count]; 
} 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if (tableView==self.myTableView) 
{ 
    return [array count]; 
} 
else if(tableView==self.myTableView2) 
{ 
    return [array2 count]; 
} 
} 
+0

Какое поведение вы сейчас видите и что вы ожидаете от этого? – jszumski

+0

Вы связали имя CellIdentifier на раскадровке? –

+0

В настоящее время все мои данные, которые состоят из 3 ячеек, повторяются снова и снова. – Greg

ответ

1

Решено!

Задача №1: NSDictionary Цикл «для» не нужен и вносил вклад в несколько записей. Я удалил петлю.

Задача №2: numberOfSectionsInTableView и numberOfRowsInSection. Отредактированная версия ниже. Извлеченный урок: вы хотите, чтобы numberOfSectionsInTableView отображал 1, если вам нужен только один экземпляр вашего массива. Значение 2 приведет к отображению двух экземпляров, и так далее и т. Д.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
if (tableView==self.myTableView) 
{ 
    return [array count]; 
} 
else if(tableView==self.myTableView2) 
{ 
    return 1; 
} 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if (tableView==self.myTableView) 
{ 
    return 5; 
} 
else if(tableView==self.myTableView2) 
{ 
    return [array2 count]; 
} 
} 
Смежные вопросы