Uitableview 使用故事板在xamarin ios中创建和使用自定义原型表格单元格
是否可以使用故事板在xamarin ios(monotouch)中创建和使用“自定义”原型表格单元 我只能在Stuart Lodge解释使用xib/nibs的方法时找到:Uitableview 使用故事板在xamarin ios中创建和使用自定义原型表格单元格,uitableview,xamarin.ios,prototype,Uitableview,Xamarin.ios,Prototype,是否可以使用故事板在xamarin ios(monotouch)中创建和使用“自定义”原型表格单元 我只能在Stuart Lodge解释使用xib/nibs的方法时找到: 让我们来回答这个问题!我也在找这个。:) 1) 打开具有ViewController和TableView的情节提要: 添加原型单元(如果之前没有添加单元): 根据需要自定义单元格(在我的示例中,有自定义UIImage和Label): 记住设置单元格的高度。要执行此操作,请选择整个TableView,并从“属性”窗口中选择“
让我们来回答这个问题!我也在找这个。:) 1) 打开具有ViewController和TableView的情节提要: 添加原型单元(如果之前没有添加单元): 根据需要自定义单元格(在我的示例中,有自定义UIImage和Label): 记住设置单元格的高度。要执行此操作,请选择整个TableView,并从“属性”窗口中选择“布局”选项卡。在“属性”窗口的顶部,您应该看到“行高”-输入适当的值: 现在再次选择原型单元。在“属性”窗口中键入类的名称(它将为其创建代码隐藏类)。在我的例子中,这是“FriendsCustomTableViewCell”。在此之后,为您的单元格提供“标识符”。正如你所看到的,我的手机是“FriendCell”。最后要设置的是“样式”属性设置为“自定义”。“名称”字段应为空。在键入“类”后单击“输入”后,将自动创建代码隐藏文件: 现在,单元格的代码应该如下所示:
public partial class FriendsCustomTableViewCell : UITableViewCell
{
public FriendsCustomTableViewCell (IntPtr handle) : base (handle)
{
}
public FriendsCustomTableViewCell(NSString cellId, string friendName, UIImage friendPhoto) : base (UITableViewCellStyle.Default, cellId)
{
FriendNameLabel.Text = friendName;
FriendPhotoImageView.Image = friendPhoto;
}
//This methods is to update cell data when reuse:
public void UpdateCellData(string friendName, UIImage friendPhoto)
{
FriendNameLabel.Text = friendName;
FriendPhotoImageView.Image = friendPhoto;
}
}
在UITableViewSource中,必须在类的顶部声明cellIdentifier(在我的示例中是“FriendCell”),在“GetCell”方法中,必须强制转换单元格并为其设置数据:
string cellIdentifier = "FriendCell";
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
FriendsCustomTableViewCell cell = (FriendsCustomTableViewCell) tableView.DequeueReusableCell(cellIdentifier);
Friend friend = _friends[indexPath.Row];
//---- if there are no cells to reuse, create a new one
if (cell == null)
{ cell = new FriendsCustomTableViewCell(new NSString(cellIdentifier), friend.FriendName, new UIImage(NSData.FromArray(friend.FriendPhoto))); }
cell.UpdateCellData(friend.UserName, new UIImage(NSData.FromArray(friend.FriendPhoto)));
return cell;
}
就这样,现在您可以使用自定义单元格。我希望它会有所帮助。谢谢,帮了我的忙:)尽管我的“代码隐藏”只有默认的(IntPtr句柄)构造函数