如何在ruby motion中创建textView



我刚开始使用RubyMotion,在Rails中有一段历史。我有一个简单的应用程序,有两个视图。第一个视图是一个tableView,它列出了一个"类别"数组。每个类别都有一个局部视图。我最初将详细信息视图设置为tableView,但现在改为UILabel,因为每个类别只有一小段静态文本。我曾考虑在细节中使用一行表,并根据内容更改单元格高度,但由于我只需要一个单元格,我决定。。。这可能是对表的不当使用和对UILabel的更好使用(据我所知,UILabel用于静态文本……而不仅仅是"标签"(。欢迎对此发表意见或想法。

category_view_controller.rb

因此,在我现有的category_view_controller中,我有以下方法,当从类别视图控制器中的类别列表中选择一行时,该方法会推送详细信息视图控制器。目前,我正在app_deligate.rb中静态地创建我的类别及其属性(标签和描述(。这在sim中运行良好。

   def tableView(tableView, didSelectRowAtIndexPath:indexPath)
     case indexPath.section
      when 0
       cat, label, desc = Categories.categories_list[indexPath.row]
       detailsVC = DetailViewController.alloc.initWithStyle(UITableViewStyleGrouped,        category:cat, label:label, description:desc)
       navigationController.pushViewController(detailsVC, animated:true)
     end
    tableView.deselectRowAtIndexPath(indexPath, animated:true)
  end

DetailViewController.rb

此控制器继承自UITableViewController。下面的代码已经被修改以删除tableView。。。这就是为什么没有表(您可以看到我从哪里开始更改为UILabel(。这也可以通过一个简单的白色标签加载,该标签包括我在add_deligate文件中设置的详细文本。

class DetailViewController < UITableViewController
 def initWithStyle(style, category:cat, label:label, description:desc)
   initWithStyle(style)
   @category = cat
   @description = desc
   @label_text = label
   self
 end

 def viewDidLoad
   super
   self.view.backgroundColor = UIColor.redColor
   @label = UILabel.alloc.initWithFrame([[20, 50], [280, 80]]).tap do |label|
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = @description
    label.lineBreakMode = UILineBreakModeTailTruncation
    label.numberOfLines = 0
    label.sizeToFit
    label.textColor = UIColor.colorWithHue(0.0, saturation:0.0, brightness:0.40, alpha:1.0)
    self.view.addSubview(label)
  end
 end  
end

我的困惑

我不知道如何删除tableView,只通过UILabel使用简单的静态文本。或者,也许还有更好的方法。

我知道我需要更改调用details_view_controller的categories_views_controller中的这一行。此行仍在引用UITableViewStyleGrouped。。。不应该是,因为没有表格可供设计。

detailsVC = DetailViewController.alloc.initWithStyle(UITableViewStyleGrouped, category:cat, label:label, description:desc)

我试着简单地删除"initWithStyle…"。。。但是,这会破坏应用程序。

detailsVC = DetailViewController.alloc.init(category:cat, label:label, description:desc)

我还需要details_view_controller不从<UITableViewController。这看起来就像删除或更改继承一样简单。我试过了。。。但是,这会破坏应用程序。我怀疑,一旦我从categories_view_controller中的详细信息调用中正确删除了"UITableViewStyleGrouped",这两种方法中的任何一种都可能有效。

  class DetailViewController

 class DetailViewController < UIViewController

此外,当我在details_view_controller内部的viewDidLoad方法中设置背景颜色时,它没有任何效果。不知道为什么。

 self.view.backgroundColor = UIColor.redColor

综上所述。根视图是静态类别的表视图,每个类别都有一个详细视图。当点击列表中的类别时,将加载局部视图。详细信息视图是一个简单的视图,其中有一个UILabel元素,用于显示分类详细描述。

我希望有人能为我指明学习动作的方向,并使代码按预期工作。感谢

看起来你有很多背景阅读要做。没问题——我们都在那里。但你遇到的问题似乎是对Cocoa Touch普遍不熟悉。

我建议您阅读苹果的官方指南,包括View Controller指南。

https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

如果你只想开始编码,ProMotion将是你最好的朋友。

https://github.com/clearsightstudio/ProMotion

ProMotion抽象掉了许多混乱的Objective-C,并允许您使用简单的命令打开和关闭屏幕。

class AppDelegate < ProMotion::AppDelegateParent
  def on_load
    open ListScreen.new(nav_bar: true)
  end
end
class ListScreen < ProMotion::TableScreen
  def table_data
    [{
      title: "",
      cells: [{
        title: "Cell 1",
        action: :selected_cell,
        arguments: { cell_number: 1 }
      }, {
        title: "Cell 2",
        action: :selected_cell,
        arguments: { cell_number: 2 }
      }]
    }]
  end
  def selected_cell(args={})
    if args[:cell_number] == 1
      open DetailScreen
    elsif args[:cell_number] == 2
      open OtherScreen
    end
  end
end
class DetailScreen < ProMotion::Screen
  title "Detail"
  def will_appear
    # Set up your UILabel and whatnot here
  end
end

最新更新