在我看来,我面临一个独特的问题,我已经提到了以下链接 link 1& link 2 已经但我无法完全解决我的问题,所以在这里发布我的查询。 我正在使用11种不同类型的原型单元实现40行的UITableview。其中一个原型单元有一个UILabel和一个UITextfield。现在因为我正在使用标识符重用这个原型单元格,我想做以下事情: 对于单击文本字段的第0行,我想显示一个datepicker视图。 对于第1行单击文本字段我想进行正常编辑。 对于单击文本字段的第2行,我想进行正常编辑。 对于单击文本字段的第3行,我想显示选择器视图。 并且对于特定行重复类似的模式。 我的问题在于如何获取有关委托传递给哪一行的信息。这是我的代码:
if (0 == indexPath.row)
{
cellIdentifier = @"CellLabelText";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cellLabel = (UILabel *)[cell viewWithTag:101];
cellLabel.text = [self.modelDataObj.labelTextArray objectAtIndex:indexPath.row];
textField1 = (UITextField *)[cell viewWithTag:102];
rowNum = indexPath.row;
textField1.delegate = self;
/*
placeholder text value is current date set (TBD)
*/
}
else if ((1 == indexPath.row) ||(2 == indexPath.row)||(3 == indexPath.row)||
(4 == indexPath.row) ||(6 == indexPath.row)||(10 == indexPath.row)||
(11 == indexPath.row)||(28 == indexPath.row)||(41 == indexPath.row))
{
cellIdentifier = @"CellLabelText";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cellLabel = (UILabel *)[cell viewWithTag:101];
cellLabel.text = [self.modelDataObj.labelTextArray objectAtIndex:indexPath.row];
rowNum = indexPath.row;
switch (indexPath.row) {
case 1:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 2:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 3:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 4:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 6:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 10:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 11:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 41:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
case 28:
textField1 = (UITextField *)[cell viewWithTag:102];
textField1.delegate = self;
break;
default:
break;
}
}
这是我的textShouldBeginEditing方法代码:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
NSLog(@"In textFieldShouldBeginEditing");
BOOL finalReturn;
NSLog(@"row selected is %d", rowNum);
UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:vc];
if (0 == rowNum)//)
{
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(5, 5, 300, 600)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPopoverAnimated:)];
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];
[vc.navigationItem setRightBarButtonItem:donebtn animated:NO];
[vc.view setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[vc.view addSubview:datePicker];
self.popvc = [[UIPopoverController alloc] initWithContentViewController:navc];
[self.popvc setPopoverContentSize:CGSizeMake(datePicker.bounds.size.width+(datePicker.frame.origin.y*2),250+(datePicker.frame.origin.x*2))];
//NSLog(@"Height of the picker view while embedding in popover %f", pickerView.bounds.size.height);
[self.popvc presentPopoverFromRect:textField.frame inView:textField.superview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
finalReturn = NO;
}
else if ((1 == rowNum) || (2 == rowNum))
{
finalReturn = YES;
}
else
{
finalReturn = YES;
}
return finalReturn;
}
问题是rowNum被设置为当前可见且已执行satement的行的值。因此,如果在第12行期间发生了对rowNum赋值的最后一个语句,我将无法使用它来为textFieldShouldBeginEditing中的第0行触发一个日期选择器,因为当它设置为0时,没有单击textfield并且当它被单击时rowNum被设定为12左右。 基本上我如何为第0行触发一个日期选择器,为第3行触发uipicker,对第1行和第1行进行正常编辑。 2等等。我无法解决问题,我仍在尝试各种角度。非常感谢任何帮助。
答案 0 :(得分:0)
在textFieldShouldBeginEditing中,尝试获取textField的superview,它是表视图单元格的内容视图,并获取其superview,它应该是一个表视图单元格。
之后,您可以找到该特定表视图单元格的indexPath,并根据indexPath行确定要显示的内容。