带SearchView的UITableView使新单元格在搜索时重叠旧单元格



我实现了一个SearchView来过滤UITableView中的数据。我能够成功搜索,唯一的问题是当UITableView数据被过滤时,旧的单元格仍然存在,过滤的单元格重叠,给我一个混乱的输出。根据我在SO上的回答,我已经清除了表源,但仍然没有解决问题。

下面是我要做的:

FilterController

void searchFiler_TextChanged(对象发送者,UISearchBarTextChangedEventArgs e){

    if (!string.IsNullOrEmpty(e.SearchText))
    {
        ppTable.Source = null;
        AppDelegate.filteredList = null;
        ppTable.ReloadData();
        filteredModelList = filter(dupes, e.SearchText);
        mAdapter = new PeoplePlacesSource(filteredModelList, "");
        ppTable.Source = mAdapter;
        ppTable.ReloadData();
    }
    else
    {
        ppTable.Source = null;
        mAdapter = new PeoplePlacesSource(dupes, "");
        searchFiler.TextChanged += searchFiler_TextChanged;
        mAdapter.TableRowSelected += mAdapter_TableRowSelected;
        ppTable.Source = mAdapter;
        ppTable.ReloadData();
    }
}

PeoplePlacesSource

public override UITableViewCell GetCell(UITableView tableView, Foundation.NSIndexPath indexPath)
        {

            cell = tableView.DequeueReusableCell("PeoplePlacesCell_Cell") as PeoplePlacesCell ?? PeoplePlacesCell.Create();
subList = userList.ElementAt(indexPath.Row);

            cell.UpdatePeoplePlacesCell(subList);
             return cell;
        }

PeoplePlacesCell

 internal void UpdatePeoplePlacesCell(System.Linq.IGrouping<string, Models.EmployeeModel> subList)
    {
        var yPosition =15;
        var xPosition = 10;
        var imgDefault = new UIImageView();
        imgDefault.Frame = new CoreGraphics.CGRect(10, viewParent.Frame.Y, 60,60);
        imgDefault.Image = UIImage.FromFile("ic_appicon.png");
        CALayer profileImageCircle = imgDefault.Layer;
        profileImageCircle.CornerRadius = 28;
        profileImageCircle.BorderWidth = 2;
        profileImageCircle.BorderColor = new CoreGraphics.CGColor(211, 34, 41);
        profileImageCircle.MasksToBounds = true;
        imgDefault.ClipsToBounds = true;
        viewParent.AddSubview(imgDefault);

        var labelUserName = new UILabel();
        labelUserName.Frame = new CoreGraphics.CGRect(0, 0, viewContainer.Frame.Width, 30);
        labelUserName.TextColor = UIColor.FromRGB(211, 34, 41);
        labelUserName.Text = subList.Key ;
        labelUserName.ClipsToBounds = true;
        labelUserName.Font = UIFont.BoldSystemFontOfSize(17);
        viewContainer.AddSubview(labelUserName);

        var mList = subList.Take(5);
        foreach (var employeeModel in mList)
        {
            var labelPlace = new UILabel();
            labelPlace.Frame = new CoreGraphics.CGRect(0, yPosition, viewContainer.Frame.Width, 50);
            if (employeeModel.Place.Contains(","))
            {
                labelPlace.Text = employeeModel.Place.Substring(0, employeeModel.Place.IndexOf(","));
            }
            else
            {
                labelPlace.Text = employeeModel.Place;
            }
            labelPlace.Font = UIFont.FromName(labelPlace.Font.Name, 12f);
            labelUserName.SizeToFit();
                labelPlace.ClipsToBounds = true;
            viewContainer.AddSubview(labelPlace);

           yPosition += 15;


        }
    }

我该如何解决这个问题?

滚动后也有同样的问题吗?您的单元格被重用,但我没有看到删除添加子视图的代码。在重新使用单元格之前,请尝试删除添加的所有子视图。

相关内容

最新更新