uitableview - 使用 Storyboard在 xamarin ios 中创建和使用自定义原型(prototype)表格单元格

标签 uitableview xamarin.ios prototype

是否可以使用 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 和标签):

enter image description here

enter image description here

记住设置单元格的高度。为此,请选择整个 TableView,然后从“属性”窗口中选择“布局”选项卡。在属性窗口的顶部,您应该看到“行高” - 输入适当的值:

enter image description here

现在再次选择原型(prototype)单元。在“属性”窗口中键入类的名称(它将为其创建代码隐藏类)。就我而言,这是“FriendsCustomTableViewCell”。之后为您的手机提供“标识符”。正如你所看到的,我的是“FriendCell”。最后要设置的是将“Style”属性设置为自定义。 “名称”字段应为空。输入“Class”后单击“enter”后,将自动创建代码隐藏文件:

enter image description here

enter image description here

现在单元格的代码应如下所示:

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/

相关文章:

objective-c - 原型(prototype) UITableViewCell 不在 Storyboard 中的 UITableView 顶部

c# - 使用 MonoTouch 进​​行 JSON 反序列化

ios - dequeueReusableCellWithIdentifier UITableView 两次

xamarin - 使用 Telerik 库后缺少带有目的字符串的 NSCalendarsUsageDescription 键

ios - 为什么我们的 MonoTouch 应用程序会破坏垃圾收集器?并不是内存不足

javascript - 为什么 JSON.stringify 不序列化原型(prototype)值?

JavaScript原型(prototype): how do I call a method within another method?

javascript - 在 JavaScript 中努力处理继承和原型(prototype)设计 - 原型(prototype)方法不像 "should"那样工作

ios - 如何自定义UIDatePicker并改变文字颜色和背景颜色?

ios - 为什么仍然出现 "Could not cast value of type"错误?