0

我必须制作一个应用程序,我可以在 UITableview 中添加 N 行。这就是我使用 reusability 的方式

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *CellIdentifier = @"MyCell";

        UITableViewCell *Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(Cell==nil)
        {
            Cell=[self tableViewCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath ofTableView:tableView];
        }

        [Cell setSelectionStyle:UITableViewCellEditingStyleNone];
        [Cell setAccessoryType:UITableViewCellAccessoryNone];

        [self ConfigureCell:Cell forIndexPath:indexPath forTableView:tableView];

        return Cell;
}

然后在向右滑动时,我必须在该方法中检索 UITableviewCell 后,在 UITableview 的特定单元格上画一条线并添加按钮,并且必须在添加子视图后将单元格移动到底部。

CGPoint location = [recognizer locationInView:GroupedTableView];

    NSIndexPath *IndexPath = [GroupedTableView indexPathForRowAtPoint:location];

    UITableViewCell *cell = [self.GroupedTableView cellForRowAtIndexPath:IndexPath];

  UIButton *CrossButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        CrossButton.frame=CGRectMake(250, 12, 15, 15);
        [CrossButton setBackgroundImage:[UIImage imageNamed:@"x.png"] forState:UIControlStateNormal];
        CrossButton.tag=800+IndexPath.row;
        [CrossButton addTarget:self action:@selector(CrossButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:CrossButton];

        UIImageView *LineImage=[[UIImageView alloc]init];
        LineImage.frame=CGRectMake(10, 19, 220, 2);
        LineImage.tag=700+IndexPath.row;
        LineImage.image=[UIImage imageNamed:@"line.png"];
        LineImage.userInteractionEnabled=YES;
        [cell.contentView addSubview:LineImage];


 NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:RowNum inSection:0];

    [self.GroupTableContentArray insertObject:[self.GroupTableContentArray objectAtIndex:IndexPath.row] atIndex:lastIndexPath.row+1];

    [self.GroupTableContentArray removeObjectAtIndex:IndexPath.row];

    [self.GroupedTableView moveRowAtIndexPath:IndexPath toIndexPath:lastIndexPath];

这在表格高度很小之前也可以正常工作,但是一旦它大于屏幕尺寸并且我滚动查看底部内容,我在该特定单元格上绘制的线消失了,有时它在其他未标记的单元格上可见。我知道这是可重用性的问题,但我无法想出办法。

我的要求就像 any.Do 应用程序,我们向右滑动以选择已完成的任务并将其放在表格的底部。任何帮助将不胜感激。

4

1 回答 1

1

在我看来,您似乎不太了解表格视图单元格重用的工作原理。

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

向表格委托询问将在指定索引路径处显示的单元格。你向表格视图询问一个可重用的单元格dequeueReusableCellWithIdentifier:。现在,如果返回的单元格为 nil,那么您的工作就是创建一个新单元格。

在设置单元格属性(如文本、详细文本和图像)时,您必须将每个单元格视为包含不需要的数据,这意味着您应该在每次返回单元格之前覆盖所有内容。

举个例子,假设你有一个包含 10 个可见单元格和 100 行的表格视图。您的第一个单元格中有“Hello world”,其他单元格是空的。现在,当您开始滚动时,您将每 10 个左右的单元格看到“Hello world”。发生这种情况是因为从表可重用单元队列中返回了一个随机可用单元,该队列保留了所有更改,如文本、图像和子视图。

这也是您的单元格使用线条和按钮发生的情况,因此为避免这种情况,应为每个索引路径设置适当的属性。问题是您正在将子视图添加到您的单元格而不引用它们,因此它们无法轻松删除或隐藏,因此在随机索引路径中始终可见。此外,您将遇到在同一个单元格中有多个行和按钮的情况。

最好创建一个 UITableViewCell 子类,其中每个单元格都有自己的线条和按钮,可以根据需要为每个索引路径显示/隐藏。

于 2013-10-21T08:39:25.883 回答