我這個同樣的問題,在那裏我有UITableViewCells內UITextFields並不能掙扎獲取視圖以在編輯時滾動到該字段。我的解決方案的核心在下面。
此代碼的關鍵是創建UITextField的行。它不是在CGRectMake()函數中對x和y值進行硬編碼,而是使用放置它的單元格的x和y(+/-單元邊緣所需的任何偏移量,如下所示)。在UITextField *中硬編碼的x和y值給每個單元格相同的 x,y每個UITextField *的幀位置(當它顯示時它顯然被單元格框架覆蓋),所以當你調用'scrollRectToVisible'似乎沒有它應該滾動的正確座標。
1)創建細胞,並利用細胞的幀x和y值添加的UITextField *到細胞(我包括這裏偏移,其是可選的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"UITableViewCell"] autorelease];
//this is the critical part: make sure your UITextField* frame is based on the frame of the cell in which it's being placed.
UITextField* txtField = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+20, cell.frame.origin.y+9, 280, 31)];
txtField.delegate = self;
[cell addSubview:txtField];
return cell;
}
2)在textFieldDidBeginEditing
調整滾動視圖
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [textField frame];
[self.tableView scrollRectToVisible:textFieldRect animated:YES];
}
八月,這就是我目前正在做的。問題在於,滾動需要在兩個地方發生。在tableView:didSelectRowAtIndexPath:方法和textFieldDidBeginEditing:方法中。直接點擊UITextField不會冒泡,因此無法在一個地方處理。 – Coocoo4Cocoa 2008-12-08 21:20:23
您應該讓UITextFieldDelegate方法調用tableView:didSelectRowAtIndexPath:如果兩種情況下的scrollng都相同。這是唯一合理的方法,因爲UITableView被UITextField遮擋。 – 2008-12-08 22:11:38