使用故事板在 xamarin ios 中创建和使用自定义原型表单元格 [英] Create and use custom prototype table cells in xamarin ios using storyboard

查看:36
本文介绍了使用故事板在 xamarin ios 中创建和使用自定义原型表单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用情节提要在 xamarin ios (monotouch) 中创建和使用自定义"原型表格单元格?

我只能通过 Stuart Lodge 找到这个解释使用 xib/nibs 的方法:

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

现在再次选择原型单元格.在属性"窗口中键入类的名称(它将为其创建代码隐藏类).就我而言,这是FriendsCustomTableViewCell".之后为您的单元格提供标识符".如您所见,我的是FriendCell".最后要设置的是样式"属性设置为自定义.名称"字段应为空.键入类"后单击输入"后,将自动创建代码隐藏文件:

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

公共部分类 FriendsCustomTableViewCell : UITableViewCell{public FriendsCustomTableViewCell (IntPtr handle) : base (handle){}public FriendsCustomTableViewCell(NSString cellId, stringfriendName, UIImagefriendPhoto) : base (UITableViewCellStyle.Default, cellId){FriendNameLabel.Text = 朋友名;FriendPhotoImageView.Image =friendPhoto;}//这个方法是在重用时更新单元格数据:public void UpdateCellData(stringfriendName, UIImagefriendPhoto){FriendNameLabel.Text = 朋友名;FriendPhotoImageView.Image =friendPhoto;}}

在 UITableViewSource 中,你必须在类的顶部声明 cellIdentifier(在我的例子中是FriendCell"),在GetCell"方法中你必须投射单元格并为它们设置数据:

string cellIdentifier = "FriendCell";公共覆盖 UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath){FriendsCustomTableViewCell cell = (FriendsCustomTableViewCell) tableView.DequeueReusableCell(cellIdentifier);朋友朋友 = _friends[indexPath.Row];//---- 如果没有要重用的单元格,则创建一个新单元格如果(单元格 == 空){ cell = new FriendsCustomTableViewCell(new NSString(cellIdentifier),friend.FriendName, new UIImage(NSData.FromArray(friend.FriendPhoto)));}cell.UpdateCellData(friend.UserName, new UIImage(NSData.FromArray(friend.FriendPhoto)));返回单元格;}

就是这样,现在您可以使用自定义单元格了.我希望它会有所帮助.

Is it possible to create and use 'Custom' prototype table cells in xamarin ios (monotouch) using storyboard?

I could only find this by Stuart Lodge explaining a method using xib/nibs: http://www.youtube.com/watch?feature=player_embedded&v=Vd1p2Gz8jfY

解决方案

Let's answer this question! I was also looking for this one. :)

1) Open Storyboard where you have your ViewController with TableView:

Add prototype cell (if there is no cell added before):

Customize cell as you want (in my case there is custom UIImage and Label):

Remember to set height of the cell. To do it select your whole TableView and from the Properties window select "Layout" tab. On the top of the properties window you should see "row height" - put the appropriate value:

Now select prototype cell once again. In the Properties window type the name of the class (it will create code-behind class for it). In my case this is "FriendsCustomTableViewCell". After that provide "Identifier" for your cell. As you can see my is "FriendCell". Last thing to set is "Style" property set to custom. "Name" field should be empty. Once you click "enter" after typing "Class" code-behind file will be automatically created:

Now code behind for the cell should look like below:

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;

    }
}

In UITableViewSource you have to declare cellIdentifier at the top of the class (in my case it is "FriendCell") and in "GetCell" method you have to cast cells and set data for them:

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;
}

That's it, now you can use your custom cells. I hope that it will help.

这篇关于使用故事板在 xamarin ios 中创建和使用自定义原型表单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆