• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Xamarin Monotouch CustomCell 为空白

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:03:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有以下 CustomCell:

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Zurfers.Mobile.iOS
{
    public class CustomCell : UITableViewCell
    {
        UILabel headingLabel, subheadingLabel, priceLabel;
        UIImageView imageService;
        UIImageView star, star2, star3, star4, star5;
        public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
        {
            imageService = new UIImageView();
            star   = new UIImageView();
            star2  = new UIImageView();
            star3  = new UIImageView();
            star4  = new UIImageView();
            star5  = new UIImageView();
            headingLabel = new UILabel(){
                Font = UIFont.FromName("Cochin-BoldItalic", 22f),
                TextColor = UIColor.FromRGB (127, 51, 0),
            };
            subheadingLabel = new UILabel(){
                Font = UIFont.FromName("Cochin-BoldItalic", 22f),
                TextColor = UIColor.FromRGB (127, 51, 0),
            };
            priceLabel = new UILabel(){
                Font = UIFont.FromName("Cochin-BoldItalic", 22f),
                TextColor = UIColor.FromRGB (127, 51, 0),
            };
        }

        public void UpdateCell (string title, string subtitle, string price, UIImage image)
        {
            imageService.Image = image;
            headingLabel.Text = title;
            subheadingLabel.Text = subtitle;
            priceLabel.Text = price;
        }

        public override void LayoutSubviews ()
        {
            base.LayoutSubviews ();
            imageService.Frame = new RectangleF(63, 5, 33, 33);
            headingLabel.Frame = new RectangleF(5, 4, 63, 25);
            subheadingLabel.Frame = new RectangleF(100, 18, 100, 20);
            priceLabel.Frame = new RectangleF(250, 18, 100, 20);
        }
    }
}

这是我的 TableSource:

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Zurfers.Mobile.iOS
{
    public class HotelTableSource : UITableViewSource
    {

        string[] tableItems;
        NSString cellIdentifier = new NSString("TableCell");

        public HotelTableSource (string[] items)
        {
            tableItems = items;
        }
        public override int RowsInSection (UITableView tableview, int section)
        {
            return tableItems.Length;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show();
            tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight
        }

        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {

            CustomCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomCell;
            if (cell == null)
                cell = new CustomCell(cellIdentifier);
            cell.UpdateCell(tableItems[indexPath.Row], tableItems[indexPath.Row], tableItems[indexPath.Row],
                            UIImage.FromFile (tableItems[indexPath.Row]) );
            return cell;


        }
    }
}

但是当我运行应用程序时,TableView 是空的。在我创建 TableView 之前,每个 CustomCell 都有一个值。

我做错了什么?

提前感谢您的帮助。



Best Answer-推荐答案


Cell 不仅应该包含控件,还应该放在它上面(或者它的 ContentView 上)。为您添加到自定义单元格的任何控件调用 AddSubview:




如果不起作用,请尝试使用 UITableViewCell 的默认控件(TextLabelDetailTextLabelImageView) 来显示一些数据。如果出现数据,则您的自定义 UITableViewCell 中仍有问题(错误的帧,空文本),如果没有,则问题出在 UITableViewSource 中(空数据)。

关于ios - Xamarin Monotouch CustomCell 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17457598/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap