Tips
Last active
July 17, 2018 05:04
-
-
Save JeOam/9b23f9539549479e61a3 to your computer and use it in GitHub Desktop.
UITableView Tips
UITableView Scroll to Bottom:
NSInteger sectionCount = [self.chatDataArray count];
if (sectionCount > 0) {
NSInteger cellsCount = [(NSArray *)[self.chatDataArray objectAtIndex:(sectionCount - 1)] count];
if (cellsCount > 0) {
NSIndexPath* lastCellPath = [NSIndexPath indexPathForRow: cellsCount-1 inSection: sectionCount-1];
[chatTableView scrollToRowAtIndexPath:lastCellPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
Swipe to Delete 相关设置:
ourTableView.allowsMultipleSelectionDuringEditing = NO; // Swipe to Delete Setting
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[ourTableView removeObjectAtIndex:indexPath.row];
[ourTableView reloadData];
// 更新相关数据
}
}
Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:] 的报错原因:
Because tableView:cellForRowAtIndexPath:
is returning a null value and then configureCellForDisplay
is expecting a UITableViewCell.
在 tableView 的 style 为 Grouped 时,设置:tableView:heightForHeaderInSection:
和 tableView:heightForFooterInSection:
返回值为 0 可能无效,需设置成 0.01。
TableView 不显示没内容的 Cell:
self.tableView.tableFooterView = [[UIView alloc] init];
获取当前 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));
}
遇到一个使用 tableView estimatedHeightForRow 时的坑:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
使用了上面的方法,可能导致在 iOS 8 上调用
[self.navigationController pushViewController:vc animated:YES];
时,tableView 会自动滚动到顶部,无法禁止。
参考:
Reload 部分 TableView:
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
设置 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];
}
}
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
中去掉 UITableViewCell 之间的横线:
tableView.separatorStyle = NO;
Disable the UITableView selection highlighting: