2012-04-14 114 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節是正確的