【问题标题】:Cannot update label with new date inside tableview cell with datepicker无法使用 datepicker 在 tableview 单元格中使用新日期更新标签
【发布时间】:2016-01-06 13:07:24
【问题描述】:

我在视图控制器中有一个 tableview。我的计划是,在我的 tableview 单元格内是一个标签,当用户点击 tableview 单元格时,UIAlertView ActionSheet 会出现,里面有一个 datepicker。在经历了很多挫折之后,我最终在 github 上使用了一个名为 LGAlertView 的 repo。这似乎是一个绝佳的选择!它很漂亮,似乎工作顺利。它里面有一个日期选择器。但是它只工作一次,然后它不能再次更新我的标签。意思是,我可以在第一次选择单元格时设置日期,它会完美地更新我的单元格中的标签,但如果我要再次选择单元格并选择一个日期,它不会更新我的标签。

更多信息:

我的标签在故事板中的标签为 100。

//-------------------------------------------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------

{
    if (indexPath.row == 0)
    {
        datePicker = [UIDatePicker new];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.frame = CGRectMake(0.f, 0.f, self.view.frame.size.width, 110.f);
        [[[LGAlertView alloc] initWithViewAndTitle:@"What Day Did You Get Married"
                                           message:@"Tap Done When Finished"
                                             style:LGAlertViewStyleActionSheet
                                              view:datePicker
                                      buttonTitles:@[@"Done"]
                                 cancelButtonTitle:@"Cancel"
                            destructiveButtonTitle:nil
                                     actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index){
                                         selectedDate = datePicker.date;
                                         NSDateFormatter *df = [[NSDateFormatter alloc] init];
                                         df.dateStyle = NSDateFormatterLongStyle;
                                         UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AnniversaryCell" forIndexPath:indexPath];
                                         UILabel *dateLabel = (UILabel*) [cell viewWithTag:100];
                                         dateLabel.text = [NSString stringWithFormat:@"Married Since: %@",[df stringFromDate:selectedDate]];
                                         [cell setSelected:NO];

                                         NSLog(@"actionHandler, %@, %lu", title, (long unsigned)index);
                                     }
                                     cancelHandler:^(LGAlertView *alertView) {
                                         NSLog(@"cancelHandler");
                                     }
                                destructiveHandler:^(LGAlertView *alertView)
                                {
                                    NSLog(@"destructiveHandler");
                                }]
         showAnimated:YES completionHandler:nil];

    }

}

【问题讨论】:

    标签: ios objective-c xcode datepicker


    【解决方案1】:

    修改代码

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AnniversaryCell" forIndexPath:indexPath];
    

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多