数据仅填充在UITableViewCell中的最后一个UITextField中

时间:2013-10-08 10:20:45

标签: rubymotion

我的tableview在accessoryView位置有名称和文本字段。使用以下UITableViewDelegate方法,我调用onEdit并调用UIPickerView。问题是,如果我有超过1个单元格,它只会填充最后一个uitextfield中的数据,无论我点击哪个文本字段。如何允许数据进入我正在编辑的文本字段?

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

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



  picker = UIPickerView.alloc.initWithFrame([[0, self.view.frame.size.height / 2 + 50], [self.view.frame.size.width, self.view.frame.size.height]])
  picker.delegate = self
  picker.showsSelectionIndicator = true

  @picker_field = UITextField.alloc.initWithFrame([[200,20],[150,40]])
  @picker_field.placeholder = "Choose Location.."
  @picker_field.delegate = self
  @picker_field.inputView = picker


  cell.textLabel.text = @list[indexPath.row]
  cell.accessoryView = @picker_field
  cell.selectedBackgroundView.backgroundColor = UIColor.redColor




  #cell.selectionStyle = UITableViewCellSelectionStyleNone

cell
end

  def pickerView(pickerView, numberOfRowsInComponent: component)
@choices_count
  end

  def  pickerView(pickerView, titleForRow: row, forComponent: component)
@choices[row]
  end

  def pickerView(pickerView, didSelectRow: row, inComponent: component)
    @picker_field.text = @choices[row]
    @picker_field.resignFirstResponder

  end

1 个答案:

答案 0 :(得分:0)

解决方案来自于更加关注@picker_field成为第一个响应者。一旦我发出if语句,说明firstResponder,它每次都会填充。

  def pickerView(pickerView, didSelectRow: row, inComponent: component)
    if @picker_field = firstResponder
      @picker_field.text = @choices[row]
      @picker_field.resignFirstResponder
    end
   end