【问题标题】:Can we make the properties in a Custom UITableViewCell as public我们可以将自定义 UITableViewCell 中的属性设为公开吗
【发布时间】:2018-04-21 06:14:06
【问题描述】:

我创建了一个自定义 UITableViewCell,在 xib 上我添加了 3 个 UILabel,也为其添加了插座。在自定义类中,我可以访问所有这三个。是否有可能对这些项目有一个公共访问说明符?

【问题讨论】:

  • 如果对您有帮助,请接受答案。或者回答你自己的问题。知道这个问题已解决将有助于其他人在未来面临同样的问题。

标签: xamarin.ios xib custom-cell


【解决方案1】:

是的,您可以这样做。在您的自定义 UITableViewCell 类中创建一些公共访问属性以将标签暴露给外部世界,如下所示:

public class YourCustomCell : UITableViewCell
{
   public UILabel MyFirstLabel
   {
      get { return myFirstLableOutletInstance; }
   }

   public UILabel MySecondLabel
   {
      get { return mySecondLableOutletInstance; }
   }
}

然后在这个类之外,你实例化这个单元对象,你可以通过:cell.MyFirstLabel;

例如。如果你想设置这个标签的TextColor,你可以这样做:

cell.MyFirstLabel.TextColor = UIColor.Red;
cell.MySecondLabel.TextColor = UIColor.Blue;

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    您可以将插座从设计器文件移动到您的 CustomViewCell 并将它们标记为公开。

    被移动的Outlet不会在设计器中再次生成,所以删除它们并删除GeneratedCode属性是保存。

    [Outlet]
    public UILabel Title { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2016-08-02
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多