Reputation: 138
I have a tableview with 2 textbox.text1 loads data on viewdidload function. for text2, the input is dates. I am using tableview didselectrow method to show datepicker. but when i select date from datepicker, it is printing in console but not added in textbox.
` override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndex = indexPath.row
let screenSize: CGRect = UIScreen.mainScreen().bounds
datePickerContainer.frame = CGRectMake(20, 200, 290, 200.0)
datePickerContainer.backgroundColor = UIColor.whiteColor()
datePickerContainer.layer.borderWidth = 2.0
datePickerContainer.layer.borderColor = UIColor.lightGrayColor().CGColor
var pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero)
datePicker.frame = CGRectMake(0.0, 20, pickerSize.width, 160)
//datePicker.setDate(NSDate(), animated: true)
//datePicker.maximumDate = NSDate()
datePicker.datePickerMode = .Date
datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged)
datePickerContainer.addSubview(datePicker)
var doneButton = UIButton()
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside)
doneButton.frame = CGRectMake(130.0, 160, 70.0, 37.0)
datePickerContainer.addSubview(doneButton)
self.view.addSubview(datePickerContainer)
}
func dateChangedInDate(sender:UIDatePicker){
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
let selectedDate = dateFormatter.stringFromDate(datePicker.date)
firstIndex = NSIndexPath(forRow: selectedIndex, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(firstIndex) as! mytableviewcell
cell.dateText.text = dateFormatter.stringFromDate(datePicker.date)
// print(selectedDate)
}
` Any help is appreciated....
Upvotes: 0
Views: 1226
Reputation: 168
You should try making all updates to cells inside the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
method. Make it so that the text
of your label is bound to the DatePicker
value. Then, inside your dataChangedWithDate()
method, trigger a table update of the row in question using table.reloadRowsAtIndexPath()
. Note that I'm assuming selectedIndex
is an instance variable for your controller, so that you have access to it inside the dateChangedWithDate()
method.
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Dequeue the cell, perform all of your other tasks for laying out the cell
// {...}
// Bind the label's text to the DatePicker value
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
cell.dateText.text = dateFormatter.stringFromDate(datePicker.date)
}
func dateChangedInDate(sender:UIDatePicker){
// Trigger table update for the selected row
let indexPath = NSIndexPath(forRow: selectedIndex, inSection:0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
Upvotes: 1
Reputation: 849
create Variable to store data value.
// var dateTime = ""
//cellForIndexMethod
cell.dateText.text = dateTime
func dateChangedInDate(sender:UIDatePicker){
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "yyyy-MM-dd"
dateTime = dateFormatter.stringFromDate(datePicker.date)
let indexPath = NSIndexPath(forRow: selectedIndex, inSection:0)
//Reload table
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
Upvotes: 0