我知道即时提出一个非常常见的问题。但在经历了很多类似的问题和文章后我无法理解
如何在didSelectRowAtIndexPath委托方法中访问UITableViewCell内的UITextfield。
我发布下面的代码。我可能不会按照预期的方式去...但
我想在didSelectRowAtIndexPath中创建textfield(所选行)[becomeFirstResponder] 这样我就可以在下面的文本字段中显示uiPicker。请帮帮我。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(self.isTableForHospital)
{
UIView *myview=[self createRowViewForHospitalTable:[customArray objectAtIndex:indexPath.row]];
myview.tag=indexPath.row;
[cell addSubview:myview];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
else
{
cell.textLabel.text=[customArray objectAtIndex:indexPath.row];
}
}
return cell;
}
-(UIView *)createRowViewForHospitalTable:(NSString *)rowText
{
UIView *hospital_row_view=[[UIView alloc]init];
UILabel *lblRowText=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
lblRowText.text=rowText;
txtDepartment=[[UITextField alloc]initWithFrame:CGRectMake(310, 0, 185, 30)];
//...rest styling for textfield goes here..
txtRole=[[UITextField alloc]initWithFrame:CGRectMake(495, 0, 185, 30)];
[hospital_row_view addSubview:lblRowText];
[hospital_row_view addSubview:txtDepartment];
[hospital_row_view addSubview:txtRole];
return hospital_row_view;
}
- (void)textFieldDidEndEditing:(UITextField *)textField { }
- (void)textFieldFinished:(id)sender{
NSString *value=@"sa";
[sender resignFirstResponder];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
UIView *myview=[cellSelected.contentView viewWithTag:indexPath.row];
for(UIView *item in [myview subviews])
{
if([item isKindOfClass:[UITextField class]])
{
[item becomeFirstResponder];
}
}
}
答案 0 :(得分:0)
在迭代单元子视图和获取UITextField方面,我想我有你的答案。
我在另一个帖子here上找到了这个,并将其修改为更通用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITextField* textField = [[UITextField alloc] init]; UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; UIView* subview = [[cell.contentView subviews] lastObject]; // Make sure your UITextField really *is* the last object you added. if ([subview isKindOfClass:[UITextField class]]) { textField = (UITextField*)subview; } [textField becomeFirstResponder]; }
...其中“textField”是实际文本字段对象的占位符。 希望这有帮助!
答案 1 :(得分:0)
我认为你不需要为文本字段分配init,因为它最终会指向单元格的文本字段(如果有的话)。
我相信你是在不必要地分配它。