2012-04-14 116 views
0

我创建了一个uitableview,当我启动它时,它看起来应该是这样,但是当我滚动时,它会将文本放在我没有指定的所有其他节中,是否有人请帮忙。 附加的第一张图片是它应该看起来的样子。第二它是什么,当我滚动。UITableView单元格标签在滚动时左右移动

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0){ 
     return 1;} 
    else if (section == 1){ 
     return [cellLabels count]; 
    }else if (section == 2){ 
     return 1; 
    }else{ 
     return 0; 
    } 

} 

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

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 
    tableView.showsVerticalScrollIndicator = NO; 
    // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 


    if(indexPath.section == 0) 
    { 
    } 
    else if (indexPath.section == 1) 
    { 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.textColor = [UIColor blackColor]; 
     // headerLabel.font = [UIFont SystemFontOfSize:16]; 
     [cell.textLabel setFont:[UIFont fontWithName:@"Arial" size:14]]; 
     cell.textLabel.text = [cellLabels objectAtIndex:indexPath.row]; 


    } 
return cell; 

} 

This is what is looks like when it is launched, and what it is supposed to look like

enter image description here

回答

1

你的问题很简单,

因为表视图中重新使用分配的小区, 当谈到第一次到首节你显示什么,在第二显示您的自定义文本的部分

当它向下滚动时,c青梅回它会出现在第一部分中的文本,因为当它达到

if(indexPath.section == 0) 
{ 
} 

它不会做任何事情,所以

使其

if(indexPath.section == 0) 
{ 
    cell.textLabel.text = @""; 
} 
else if(indexPath.section == 2) 
{ 
    cell.textLabel.text = @""; 
} 

if(indexPath.section == 0) 
{ 
    cell.textLabel.text = nil; 
} 

else if(indexPath.section == 2) 
{ 
    cell.textLabel.text = nil; 
} 

另一个用于第1节是正确的