2013-07-04 58 views
1

如何在rubymotion中創建多個分組的UITableView部分?如何在rubymotion中創建多個分組的UITableView部分?

下面的代碼不完整。我需要幫助確定不同的UITavleViewDataSource方法如何相互協作。

class TableController < UIViewController 

    def viewDidLoad 
     super 
     self.title = "TableView" 
     @table = UITableView.alloc.initWithFrame(self.view.bounds, 
      style: UITableViewStyleGrouped) 
     view.addSubview(@table) 

     @table.dataSource = self 
     @table.delegate = self 

     @data = ['First Name','Last Name','Birthdate', ''] 
     @datatwo = ['Locations','Allergens'] 

    end 

    def tableView(tableView, numberOfSectionsInTableView: tableView) 
     2 
    end 

    def tableView(tableView, numberOfRowsInSection: section) 
     # ***I'm not sure if I should list both my arrays above*** 
    end 

    def tableView(tableView, cellForRowAtIndexPath: indexPath) 
     @reuseIdentifier ||= "CELL_IDENTIFIER" 

     cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier) 
     cell = UITableViewCell.alloc.initWithStyle(
       UITableViewCellStyleDefault, 
       reuseIdentifier: @reuseIdentifier) 

     @text = UITextField.alloc.initWithFrame([[140, 12], [160, 50]]) 

     if (indexPath.section) == 0 

      if (indexPath.row) == 0 
       @first = @text 
       @first.placeholder = "First Name" 
       @first.textAlignment = UITextAlignmentRight 
       cell.addSubview(@first) 
      elsif (indexPath.row) == 1 
       @last = @text 
       @last.placeholder = "Last Name" 
       @last.textAlignment = UITextAlignmentRight 
       cell.addSubview(@last) 

      end 
     end 

     if (indexPath.section) == 1 
         if (indexPath.row) == 0 
       @location = @text 
       @location.placeholder = "Location" 
       @location.textAlignment = UITextAlignmentRight 
       cell.addSubview(@location) 
      elsif (indexPath.row) == 1 
       @kidallergens = @text 
       @kidallergens.placeholder = "" 
       @kidallergens.textAlignment = UITextAlignmentRight 
       cell.addSubview(@kidallergens) 
      end 
     end 


     cell.textLabel.text = @data[indexPath.row] 
     cell 
    end 

    def tableView(tableView, didSelectRowAtIndexPath: indexPath) 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    end 
end 
+1

如果你看看ProMotion或Formotion,兩者都有分組表格樣式,你可以用一個簡單的哈希來做到這一點。但我在下面回答了您的具體問題。 –

回答

1

使用section參數來確定要返回的編號。

def tableView(tableView, numberOfRowsInSection: section) 
    if section == 0 
    @data.length 
    elsif section == 1 
    @datatwo.length 
    else 
    0 
    end 
end 
+0

謝謝Jamon。我很感謝你的回答。我一直在想。另外,我對RubyMotion很陌生。謝啦。 – rife

+0

沒問題。如果有任何問題,請隨時將我的答案標記爲正確。 :) –

相關問題