最新更新:
是不是常常遇到,tableview 中有輸入框的需求,你會怎么做,直接修改tableview 的y, scroll to某一index? 給大家說一個可精準(zhǔn)定位滾動位置的方法: textfield 成為焦點(diǎn)時,會觸發(fā)他的代理方法:
<pre><code>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//獲取當(dāng)前輸入的textfield,在tableview 的位置
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:_detailTableView];
if (pointInTable.y > (ScreenHeight-KeyboardHeight)) {
CGPoint contentOffset = _detailTableView.contentOffset;
contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
[_detailTableView setContentOffset:contentOffset animated:YES];
}
}
</code></pre>
就是這么簡單!!!
1,設(shè)置UITableView分割線從頂端開始
<pre><code>
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
</code></pre>
2,設(shè)置UITableView空白數(shù)據(jù)多余的分割線
<pre><code>
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
</code></pre>
3,當(dāng)UITableViewCell 使用autoLayout之后,我們知道UILabel設(shè)置numberOfLines 為0 時,UILabel 可以自動換行.那么問題來了,自動換行之后,cell的高度也會變,如何計(jì)算cell的高度呢? 有一個高大上的方法:
在cell 中調(diào)用可直接返回它的size
<pre><code>
CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
</code></pre>
4,UITableViewCell的xib中也可以設(shè)置分割線的Inset