简体   繁体   English

在“表视图”中,滚动通过“表视图”时,标签将与其他文本重叠。

[英]In Table View , label is getting overlapped by other text when scrolled through tableview .

滚动标签重叠后

滚动浏览表视图之前

I have added custom label to the tableview . 我已经将自定义标签添加到tableview中。 But When i scroll it looks like the given picture . 但是当我滚动时,它看起来像给定的图片。

I am adding the code too . 我也添加了代码。 `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { `-(void)prepareForSegue:(UIStoryboardSegue *)segue发送者:(id)sender {

if ([segue.identifier isEqualToString:@"DetailPlacePage"]) {
    NSIndexPath *indexPath = [self.placeTableViewController indexPathForSelectedRow];
    NomadPlaceDetailViewController *placedetailcontroller = segue.destinationViewController;
    placedetailcontroller.myDictionary = [self.placeArray objectAtIndex:indexPath.row];
}

} ` }`

This is because of your cell identifier try this code. 这是因为您的单元格标识符请尝试此代码。 Hope this may help you. 希望这对您有帮助。

- (UITableViewCell *) tableView:(UITableView *) tableView1 cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{

static NSString *CellIdentifier = nil;

   UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
   return cell;

}

When you are using dequeueReusableCellWithIdentifier: then it is best practice to reuse it's subviews also if you are adding additional subviews in cell. 当您使用dequeueReusableCellWithIdentifier时:如果在单元格中添加其他子视图,则最好的做法是重用其子视图。

You can write your cellForRowAtIndexPath like following: 您可以像下面这样编写cellForRowAtIndexPath:

- (UITableViewCell *) tableView:(UITableView *) tableView1 cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UITextField *myView;

    if (cell == nil)
    {        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        myView = [[UITextField alloc] init];  // Set frame as required
        myView.tag = indexPath.row;
        [cell.contentView addSubview:myView];
    }

    myView = [cell.contentView viewWithTag:indexPath.row];
    myView.text = @"YourText";
   return cell;

}

What happens here is it adds myView only once when cell is nil , and after that it will reuse previously added myView to set Text so myView will not be added multiple times and that solves problem. 这里发生的是它增加了myView ,只有当细胞是一次nil ,之后它会重新使用先前加入myView设置文本,以便myView不会被多次添加和解决问题。

This is because you initialize and adding the labels outside of if(cell == nil) condition. 这是因为您在if(cell == nil)条件之外初始化并添加了标签。 So every time you scroll the table view, the code for adding labels executed and new labels creating. 因此,每次滚动表视图时,都会执行添加标签和创建新标签的代码。 So just place them inside of your if(cell == nil) condition. 因此,只需将它们放在if(cell == nil)条件内。 Only data displaying section should place outside if(cell == nil) . 仅数据显示部分应放在if(cell == nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM