简体   繁体   English

小故障/错误与滚动表视图

[英]Glitch / bug with table view on scroll

I'm getting a weird glitch / bug with my UITableView. 我的UITableView出现奇怪的故障/错误。 Everything load up fine, however, when I scroll down to the second cell and back to the first, the label's text in the first cell becomes the same as in the second cell… I'm not really sure why this is happening can you kindly look at my code and help me out. 一切正常,但是,当我向下滚动到第二个单元格并返回第一个单元格时,第一个单元格中的标签文本与第二个单元格中的文本相同...我不确定为什么会发生这种情况。查看我的代码并帮助我。

Code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifer = [NSString stringWithFormat:@"CellIdentifier%i",num];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];

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

    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,20)];
    titleL.text = myTitle;

    [cell addSubview:titleL];

    return cell;
}
-(void) makeMeADreamer {
    for (int i = 0; i < arr.count; i++) {
        PFQuery *query = [PFQuery queryWithClassName:@"DreamBits"];
        [query whereKey:@"objectId" equalTo:[arr objectAtIndex:i]];

        [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
            if (!object) {
            } else {
                myTitle = [object objectForKey:@"title"];
                num = i;
                [feed beginUpdates];
                [feed reloadRowsAtIndexPaths:myArr withRowAnimation:UITableViewRowAnimationAutomatic];
                [feed endUpdates];
            }
        }];
    }
}

Try to replace this code: 尝试替换此代码:

UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,20)];
    titleL.text = myTitle;

    [cell addSubview:titleL];

to the willDisplayCellForRowAtIndexPath: method. willDisplayCellForRowAtIndexPath:方法。 Your have the same label text in both cells because your second cell is dequeued from first cell with all its subviews. 您在两个单元格中具有相同的标签文本,因为第二个单元格及其所有子视图都从第一个单元格中出队。

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString* cellId = @"messagesCell";
     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellId];
     if (!cell)
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

     return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,20)];
    titleL.text = myTitle;

    [cell addSubview:titleL];
}

You create the tableViewCell in the wrong way. 您以错误的方式创建tableViewCell You create a label and add it into the cell each time the table view reload. 您创建一个标签,并在每次重新加载表视图时将其添加到单元格中。 So you may see many label if you scroll down and up. 因此,上下滚动可能会看到很多标签。 You should only create one label and re-use it. 您应该只创建一个标签并重新使用它。 Try the following code 试试下面的代码

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,20)];
    titleL.tag = 1234;
    [cell.contentView addSubview:titleL];
}

UILabel *titleL = (UILable*)[cell.contentView viewWithTag:1234];
titleL.text = myTitle;

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

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