UITableViewCell 中的 UIDatePicker 更新 UITableViewCell textlabel
UIDatePicker in UITableViewCell update UITableViewCell textlabel
我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当 select 在其上方编辑 UITableViewCell 时显示,然后当您 select UITableView 中的任何其他 UITableViewCell 时崩溃。
我通过查看其他人的大量文档以及低于平均水平的苹果来创建此代码 UIDateCell example。
到目前为止我已经做到了
- 显示和隐藏单元格
- 将 UIDatePicker 添加到单元格
我不确定该怎么做的一件事是,一旦日期发生变化,我如何将该日期传递回 UIDatePicker 单元格上方的 UITableViewCell UILabel?
我正在用 selector 方法捕获日期。
这就是代码的样子。
- (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 中,如果索引路径是您将此内容分配给标签
的单元格,则有一个条件
在datePickerChanged中必须完成ivar的内容,并在tableview中给一个reloadData
- (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
我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当 select 在其上方编辑 UITableViewCell 时显示,然后当您 select UITableView 中的任何其他 UITableViewCell 时崩溃。
我通过查看其他人的大量文档以及低于平均水平的苹果来创建此代码 UIDateCell example。
到目前为止我已经做到了
- 显示和隐藏单元格
- 将 UIDatePicker 添加到单元格
我不确定该怎么做的一件事是,一旦日期发生变化,我如何将该日期传递回 UIDatePicker 单元格上方的 UITableViewCell UILabel?
我正在用 selector 方法捕获日期。
这就是代码的样子。
- (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 中,如果索引路径是您将此内容分配给标签
的单元格,则有一个条件在datePickerChanged中必须完成ivar的内容,并在tableview中给一个reloadData
- (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