是否可以使用 Storyboard在 xamarin ios (monotouch) 中创建和使用“自定义”原型(prototype)表格单元格?
我只能通过 Stuart Lodge 解释使用 xib/nibs 的方法找到这个: http://www.youtube.com/watch?feature=player_embedded&v=Vd1p2Gz8jfY
最佳答案
来回答一下这个问题吧!我也在找这个。 :)
1) 打开 Storyboard,其中包含带有 TableView 的 ViewController:
添加原型(prototype)单元格(如果之前没有添加过单元格):
根据需要自定义单元格(在我的例子中,有自定义 UIImage 和标签):
记住设置单元格的高度。为此,请选择整个 TableView,然后从“属性”窗口中选择“布局”选项卡。在属性窗口的顶部,您应该看到“行高” - 输入适当的值:
现在再次选择原型(prototype)单元。在“属性”窗口中键入类的名称(它将为其创建代码隐藏类)。就我而言,这是“FriendsCustomTableViewCell”。之后为您的手机提供“标识符”。正如你所看到的,我的是“FriendCell”。最后要设置的是将“Style”属性设置为自定义。 “名称”字段应为空。输入“Class”后单击“enter”后,将自动创建代码隐藏文件:
现在单元格的代码应如下所示:
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;
}
就是这样,现在您可以使用自定义单元格了。我希望这会有所帮助。
关于uitableview - 使用 Storyboard在 xamarin ios 中创建和使用自定义原型(prototype)表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072250/