UITateViewCell中的UIDatePicker更新UITableViewCell文本标签

时间:2015-03-01 00:50:04

标签: ios objective-c uitableview uidatepicker

我一直在UItableViewCell中设置UIDatePicker的过程,当在UITableView上选择任何其他UITableViewCell时,在其上面选择UITableViewCell时会显示该过程。

我创建了这段代码,其中包括来自其他人的大量文档以及低于平均值UIDateCell example的苹果。

到目前为止,我已经完成了这个

  • show&隐藏单元格
  • 将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

2 个答案:

答案 0 :(得分:1)

有一个用于存储您想要提供标签的内容的ivar。在你的cellForRowAtIndexPath中你有一个if,其中,如果indexpath是你将这个内容分配给标签的单元格

在datePickerChanged中,您必须完成ivar的内容并在tableview中提供reloadData。

答案 1 :(得分:0)

- (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 = [NSDateFormatter localizedStringFromDate:datePickerForCell.date 
                                                  dateStyle:NSDateFormatterShortStyle 
                                                  timeStyle:NSDateFormatterFullStyle];

        } 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
相关问题