我在表格视图中具有单元格中的图像
但是当数据显示在TableView中并滚动时,它并不平滑。但是我认为所有图像都已下载。
这是TableView源代码
public class ExperienceSource : UITableViewSource
{
//UINavigationController instance, to pass it to constructor
private UINavigationController primNav { get; set; }
private UITableView tableView { get; set; }
List<Experience> TableItems;
ExperienceTableViewController owner;
public ExperienceSource(List<Experience> items,ExperienceTableViewController owner, UINavigationController nav)
{
TableItems = items;
this.owner = owner;
primNav = nav;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (TableItems.Count == 0)
{
var noDataLabel = new UILabel
{
Text = "No Experiences at your location at this time. Try to change destination",
TextColor = UIColor.Black,
TextAlignment = UITextAlignment.Center,
LineBreakMode = UILineBreakMode.WordWrap,
Lines = 0
};
tableview.BackgroundView = noDataLabel;
tableview.SeparatorStyle = UITableViewCellSeparatorStyle.None;
return TableItems.Count;
}
else
{
tableview.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;
return TableItems.Count;
}
}
public override async void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var selectedExperience = await ExperienceMethods.GetSelectedTour(TableItems[indexPath.Row].id);
if (selectedExperience == "Saved")
{
ExperienceDetailViewController ExperienceDetailController = primNav.Storyboard.InstantiateViewController("ExperienceDetailViewController") as ExperienceDetailViewController;
primNav.PushViewController(ExperienceDetailController, true);
}
else
{
UIAlertController okAlertController = UIAlertController.Create("", "Cannot select this experience", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
}
tableView.DeselectRow(indexPath, true);
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell("cell_id", indexPath) as ExperienceCell;
Experience item = TableItems[indexPath.Row];
cell.UpdateCell(item);
return cell;
}
我在此类中有自定义单元格的代码
public partial class ExperienceCell : UITableViewCell
{
public ExperienceCell (IntPtr handle) : base (handle)
{
}
internal void UpdateCell(Experience experience)
{
UIImage picture;
try
{
var image_url = "https://xplorpal.com/" + experience.cover_image.img_path + "/300x300/" + experience.cover_image.img_file;
using (var url = new NSUrl(image_url))
using (var data = NSData.FromUrl(url))
picture = UIImage.LoadFromData(data);
ExperienceImage.Image = picture;
}
catch { }
finally
{
ExperienceTitle.Text = experience.title;
ExperiencePrice.Text = "$" + experience.price;
}
}
}
我的问题在哪里?如何优化此TableView的滚动?
感谢您的帮助。
答案 0 :(得分:1)
我不熟悉在TableViewCell中使用NSData.FromUrl方法,但是在大多数情况下,手动处理远程图像是一个坏主意。
您应该考虑使用SDWebImage之类的库,该库将为您处理下载,缓存并将图像分配给UIImage。