gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:15:35 27 4
gpt4 key购买 nike

我一直在经历在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com