2013-04-15 76 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“for”循环是不必要的,并且对多个条目有贡献。我删除了循环。

问题2:numberOfSectionsInTableViewnumberOfRowsInSection。下面编辑版本。获得的经验:如果您只希望显示数组的一个实例,您希望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]; 
} 
}