ios - UITableViewCell 中的 UIDatePicker 更新 UITableViewCell 文本标签

标签 ios objective-c uitableview uidatepicker

我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当选择其上方的 UITableViewCell 时会显示该 UIDatePicker,然后在选择 UITableView 中的任何其他 UITableViewCell 时会折叠。

我通过查看其他人的大量文档以及低于平均水平的苹果来创建此代码 UIDateCell example .

到目前为止我已经做到了

  • 显示和隐藏单元格
  • 将 UIDatePicker 添加到单元格

我不确定该怎么做的一件事是,一旦日期更改,我如何将该日期传递回 UIDatePicker 单元格上方的 UITableViewCell UILabel?

我在选择器方法中捕获日期。

这就是代码的样子。

- (void)viewDidLoad {
//.. set up view, UITableView (because its a subview to this view) delegate and data source etc then create UIDatePicker

//Get datepicker ready
    datePickerForCell = [[UIDatePicker alloc] init];
    datePickerForCell.datePickerMode = UIDatePickerModeDate;
    [datePickerForCell addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];

//...
}

#pragma mark UITableViewStuff
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [sheetsTableView beginUpdates];
    // typically you need know which item the user has selected.
    // this method allows you to keep track of the selection
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if (editingStartTime == true) {
                editingStartTime = false;
                [sheetsTableView endUpdates];
            } else {
                editingStartTime = true;
                [sheetsTableView endUpdates];
            }
        }
    }
}

//.. sections method
//.. rows method etc

//.. change height of cell that has picker in it to hide and show when needed
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 1) { // this is my picker cell
        if (editingStartTime) {
            return 219;
        } else {
            return 0;
        }
    } else if ((indexPath.section == 2 && indexPath.row == 0) || (indexPath.section == 3 && indexPath.row == 0)){
        return 180;
    } else {
        return self.sheetsTableView.rowHeight;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.section == 0)
    {
        if (indexPath.row == 0) {
            // Date
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            }
            cell.textLabel.text = @"Date";
            cell.detailTextLabel.text = @"1/2/17";
        } else if (indexPath.row == 1) {
            // UIPicker
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            cell.clipsToBounds = YES;
            [cell.contentView addSubview:datePickerForCell];

        }

//.. other cells below this


#pragma mark - datepicker
- (void)datePickerChanged:(UIDatePicker*) datePicker
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd MM yyyy"];


    NSString *test = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]];
    NSLog(@"%@", test);

}

所以我的问题是,如何将测试字符串(即使我尚未正确格式化)传递到indexPath (0,0) 中的UITableViewCell UITextLabel

最佳答案

有一个 ivar 用于存储您要为其添加标签的内容。在您的 cellForRowAtIndexPath 中,您有一个 if,where,if indexpath 是您将此内容分配给标签的单元格

在datePickerChanged中必须完成ivar的内容,并在tableview中给出一个reloadData。

关于ios - UITableViewCell 中的 UIDatePicker 更新 UITableViewCell 文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788802/

相关文章:

ios - 每当 admob 收到广告时,cocos2dx 动画就会滞后

objective-c - OpenGL ES 不会绘制

ios - 选择文本字段时使 UITableView 滚动

objective-c - Objective C block 和变量

ios - 如何使用 "Outlets cannot be connected to repeating content"解决此错误?

ios - 如何为 TableView 的 TableViewCell 设置 padding/margin 和圆角半径?

ios - phonegap IOS,如何打开短信并预填

ios - 导航栏在取消 "Add to Exsting Contact" Controller 时消失

ios - 将 UIButton 添加到 UIDatePicker

iOS Framework 弱链接 : undefined symbols error