在静态单元格上使用UIDatePicker
我想在静态单元格上显示从UIDatePicker中选取的日期。在静态单元格上使用UIDatePicker
问题代码如下。我想通过使用UIDatePicker更改日期来更改self.label.text
,但它不会更改。
- (IBAction)inputDate:(id)sender {
self.label.text = [NSString stringWithFormat:@"%@", self.datePicker.date];
}
- 方法
inputDate:
是有线的UIDatePicker。 - 属性
datePicker
也连线到UIDatePicker。
有什么具体的方法来改变静态单元吗?谢谢你的好心。
只有一行代码解决了这个问题。 Stunner的建议对这个最终答案非常有帮助。
- (IBAction)inputDate:(id)sender {
self.label.text = [NSString stringWithFormat:@"%@", self.datePicker.date];
[self.tableView reloadData]; // <- add this row !
}
我建议你存储想要在你的表视图改变为伊娃细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
// configure your birth date cell
self.birthDateCell = cell;
//...
}
然后你的方法看起来是这样的:
- (IBAction)inputDate:(id)sender {
self.birthDateCell.label.text = [NSString stringWithFormat:@"%@", sender.date];
}
而且应该这样做。此外,请确保您正在使用日期选择器的适当事件操作调用inputDate函数。
编辑:此外,您可能需要重新加载你的呈现,像这样的数据:
- (IBAction)inputDate:(id)sender {
self.label.text = [NSString stringWithFormat:@"%@", sender.date];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:NO];
}
'self.label'是有线以上到小区。我认为'tableView:cellForRowAtIndexPath:'方法对**静态单元**不是必需的,而且它很麻烦,因为现有单元被新单元超载。 – weed 2012-08-02 04:12:22
据我所知(我没有故事板的经验),您仍然需要为该单元格提供'tableView:cellForRowAtIndexPath:',但可以为该单元格返回一个xib加载的单元格。我编辑了我的答案,以回应你编辑的问题。 – Stunner 2012-08-02 05:45:23
非常感谢您提供** reload **表的信息。使用storyboard,我们不必编写'tableView:numberOfRowsInSection:'方法和'tableView:cellForRowAtIndexPath:'方法,'tableView:didSelectRowAtIndexPath:'方法。我认为这是故事板iOS SDK最重要的改进。 tableViewController变得非常清晰。 – weed 2012-08-02 10:12:10
值得注意的是......如果你在表视图中有多个行/部分,并且只需要更新一个单元格,调用'[tableView reloadData]'会效率低下,因为这会重新加载你的表视图的所有部分/行导致计算浪费。而是使用我在我的回答或另一个姐妹方法中建议的方法(通过单元格而不是部分重新加载)。 – Stunner 2012-08-02 11:02:49