最近項目中購物車添加了商品數量的編輯功能,so問題來了,若我們編輯的商品cell位于tableview上方,那一切ok,但是如果是底部的cell呢,到github上一通找,的確有大牛開源了三方->https://github.com/hackiftekhar/IQKeyboardManager。但是明顯大材小用了,下面談一談自己的處理
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
//textfield的tag關聯(lián)的是cell的section和row
? ? NSInteger ?section = textField.tag/1000;
? ? NSInteger ?row = textField.tag%1000;
//取出編輯的cell
? ? NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:section];
? ? UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
//將cell的坐標轉化到window上,也可以轉換到view上這個隨意
? ? UIWindow*win = [UIApplication sharedApplication].keyWindow;
? ? CGRect rect = [win convertRect:CGRectMake(0,0, cell.frame.size.width, cell.frame.size.height)fromView:cell];
? ?DebugLog(@"-----------%f",rect.origin.y);
//算偏移量,216是鍵盤高度,50是tab高度,嚴謹一點可以加鍵盤監(jiān)聽獲取高度
? ?CGFloatoffset =APP_H-216-50- rect.origin.y- rect.size.height;
? ?DebugLog(@"-_______--%f",offset);
? if(offset <=0) {
? ? ?[UIView animateWithDuration:0.3animations:^{
? ? ?CGRect frame =self.view.frame;
? ? ?DebugLog(@"-----________-----%f",frame.origin.y);
? ? ?frame.origin.y+= offset;
? ? ?self.view.frame= frame; ?
?}];
}
return yes;}
編輯結束后在shouldend里面將view還原。
還有一種實現是拿出cell,通過tableview scrolltoindexpath這個方法講cell可見,但是在textfield resignFirstResponder之后沒法回滾,需重新做動畫實現。