如何在ruby运动中创建textView?

时间:2013-04-20 21:12:28

标签: rubymotion

我刚刚开始在RubyMotion中使用Rails中的历史记录。我有一个简单的应用程序有两个视图。第一个视图是一个tableView,它列出了一个'categories'数组。每个类别都有一个详细视图。我最初设置详细视图也是一个tableView,但我改为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不继承自&lt;的UITableViewController。这似乎就像删除或更改继承一样简单。我试过......但是,这打破了应用程序。我怀疑,一旦我从categories_view_controller中的详细信息调用中正确删除了“UITableViewStyleGrouped”,其中任何一个都可能有效。

  class DetailViewController

 class DetailViewController < UIViewController

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

 self.view.backgroundColor = UIColor.redColor

所以,回顾一下。 root视图是静态类别的tableView,每个类别都有一个详细视图。点击列表中的类别时会加载详细信息视图。详细信息视图是一个简单视图,其中UILabel元素显示了类别详细说明。

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

1 个答案:

答案 0 :(得分:2)

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

我建议您阅读Apple官方指南,包括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
相关问题