cateof
cateof

Reputation: 6758

Dynamic UITableViewTable and IBOutlets

It is not allowed to have repeated content in UITableViews. We cannot connect a UILabel for example in the TableViewController as an IBOutlet. We can of course subclass the TableViewCell and set some values when the cellforRow... is called.

But how can we handle situation when an event needs to be handled? For example I have a UIDatePicker that sends the new date via an IBAction.

- (IBAction)dateChanged:(UIDatePicker *)sender {

    //how can I set the value of the sender in a UILabel?

}

The dataChanged is triggered everytime the user modifies the UI element, however I don't know how to pass the value to my UILabel since I don't have an IBOutlet for that label. In static cells I have an IBOutlet (myTxtLabel) and when the date is changed I save the value with

self.myTxtLabel = sender.value

How do we handle this case in dynamic UITableViews?

Upvotes: 0

Views: 229

Answers (2)

Bill L
Bill L

Reputation: 2836

If I'm reading your question correctly, you want change the date and then have that changed date get reflected across all the UITableView cells that have the date in them, but the problem is since that's repeated content, it can't have an outlet.

So, here's what I'd do. Keep a property for the date, when the user changes the date, in your method that gets called after changing the date, call either self.tableView reloadData or self.tableView reloadRowsAtIndexPath:withAnimation: to update just those rows. Then, in your cellForRowAtIndexPath: set the date up as normal using that property, so when you call reloadData, it is automatically updated.

Upvotes: 1

Abhinav
Abhinav

Reputation: 38162

This is my take on this:

Step 1 : Create a custom cell by subclassing UITableViewCell.

Step 2 : Set the tag property on your UIDatePicker to the value of row number.

Step 3 : Fetch the row number from tag property in your dateChanged: method call.

Step 4 : Use the row number to get the cell and update the label on it:

- (IBAction)dateChanged:(UIDatePicker *)sender {
    NSInteger rowNumber = sender.tag;
    NSIndexPath *pickerRow = [NSIndexPath indexPathForRow:rowNumber inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:pickerRow];
    cell.myLabel.text = @"the updated text";
}

Upvotes: 0

Related Questions