iOS在UITableViewCells之间显示UIPickerView [英] iOS Show UIPickerView between UITableViewCells

查看:162
本文介绍了iOS在UITableViewCells之间显示UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 7中,鼓励开发人员在需要输入时在表格单元格之间显示日期选择器,然后在完成后隐藏它们。如何实现这种效果?

In iOS 7, developers are encouraged to show date pickers between table cells when needed for input, and then hide them when done. How can I achieve this effect?

推荐答案

Vasilica Costescu在这里有一个很棒的教程:
http://masteringios.com/blog/2013/10/31/ios-7- in-line-uidatepicker /

Vasilica Costescu has a great tutorial on it here: http://masteringios.com/blog/2013/10/31/ios-7-in-line-uidatepicker/

对于静态表:
http://masteringios.com/blog/2013/11/18/ios-7-in-line -uidatepicker-part-2 /

示例代码: https://github.com/costescv/InlineDatePicker

关键位是隐藏/显示方法:

The key bits are the hide/show methods:

 - (void)showDatePickerCell {
    self.datePickerIsShowing = YES;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    self.datePicker.hidden = NO;
    self.datePicker.alpha = 0.0f;

    [UIView animateWithDuration:0.25 animations:^{
        self.datePicker.alpha = 1.0f;
    }];
}

- (void)hideDatePickerCell {
    self.datePickerIsShowing = NO;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [UIView animateWithDuration:0.25
                     animations:^{
                         self.datePicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.datePicker.hidden = YES;
                     }];
}

此UITableViewDelegate方法将通过将其高度设置为0来隐藏该行:

And this UITableViewDelegate method will "hide" the row by setting its height to 0 :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 && indexPath.row == 4 && self.datePickerIsShowing == NO){
        // hide date picker row
        return 0.0f;
    }
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

您可以通过按钮调用隐藏/显示方法,也可以选择行在表中。 (注意:如果其他行有文本字段,那么您可能需要在textFieldDidBeginEditing委托方法中隐藏datePicker)。

You can call the hide/show methods from a button or just by selecting rows in the table. (Note: If there are text fields the other rows, then you may need to hide the datePicker in the textFieldDidBeginEditing delegate method).

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 4) {
        if (self.datePickerIsShowing){
            [self hideDatePickerCell];
        }else {
            [self showDatePickerCell];
        }
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

编辑:小心使用多个内联选择器在一张表中查看。我注意到它们往往从故事板上加载速度很慢: iOS 7使用UIPickerView 打开UITableViewController的速度很慢

Be careful using more than a couple of these inline picker views on in a single table. I've noticed that they tend to load very slowly from storyboards: iOS 7 slow to open UITableViewController with UIPickerView

这篇关于iOS在UITableViewCells之间显示UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆