带有 SearchView 的 UITableView 使新单元格在搜索时与旧单元格重叠
Posted
技术标签:
【中文标题】带有 SearchView 的 UITableView 使新单元格在搜索时与旧单元格重叠【英文标题】:UITableView with SearchView makes new cells overlap old ones on search 【发布时间】:2016-06-29 08:56:32 【问题描述】:我正在实现一个 SearchView 来过滤我的 UITableView 中的数据。我能够成功搜索,唯一的问题是当 UITableView 数据被过滤时,旧单元格仍然存在并且过滤后的单元格与它们重叠,给我一个混乱的输出。我已按照我对 SO 的回答所建议的清除表源,但仍未解决问题。
这是我想要做的:
过滤控制器
void searchFiler_TextChanged(object sender, 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;
我该如何解决这个问题?任何帮助表示赞赏
【问题讨论】:
【参考方案1】:滚动后你有同样的问题吗?您的单元格被重复使用,但我没有看到删除添加的子视图的代码。在重用单元格之前,请尝试删除您添加的所有子视图。
【讨论】:
以上是关于带有 SearchView 的 UITableView 使新单元格在搜索时与旧单元格重叠的主要内容,如果未能解决你的问题,请参考以下文章