Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active July 17, 2018 05:04
Show Gist options
  • Save JeOam/9b23f9539549479e61a3 to your computer and use it in GitHub Desktop.
Save JeOam/9b23f9539549479e61a3 to your computer and use it in GitHub Desktop.
UITableView Tips
@JeOam
Copy link
Author

JeOam commented Dec 27, 2014

TableView 不显示没内容的 Cell:

self.tableView.tableFooterView = [[UIView alloc] init];

@JeOam
Copy link
Author

JeOam commented Mar 23, 2015

获取当前 cell 在 tableView 上的 Y 坐标:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
    cellRect = [self.view convertRect:cellRect fromView:tableView];
    NSLog(@"current Y is %@",@(cellRect.origin.y));    
}

@JeOam
Copy link
Author

JeOam commented Apr 16, 2015

遇到一个使用 tableView estimatedHeightForRow 时的坑:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

使用了上面的方法,可能导致在 iOS 8 上调用

 [self.navigationController pushViewController:vc animated:YES];

时,tableView 会自动滚动到顶部,无法禁止。

参考:

@JeOam
Copy link
Author

JeOam commented Apr 23, 2015

Reload 部分 TableView:

[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

@JeOam
Copy link
Author

JeOam commented Apr 23, 2015

设置 tableView 分割线的 SeparatorInset,使得分割线两边不留空隙:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

@JeOam
Copy link
Author

JeOam commented Apr 23, 2015

TableView with Index

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.indexArray objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ....
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment