无法使用datepicker更新tableview单元格中新日期的标签

时间:2016-01-06 13:07:25

标签: ios objective-c xcode datepicker

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

更多信息:

我的标签在故事板中有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];

    }

}

1 个答案:

答案 0 :(得分:1)

更改代码

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];