一, 自定义TabBarController
自定义一个TabBarController继承自UITabBarController.
@interface QPTabBarController : UITabBarController
@end
二, 重写方法
在QPTabBarController.m里重写- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
if ([item.title isEqualToString:@"沟通"]) {
QPNavigationController *nav = self.viewControllers[1];
UIViewController *controller = nav.viewControllers.firstObject;
if ([self checkIsDoubleClick:controller]) {
//通知消息列表页面,双击了tabbar
[[NSNotificationCenter defaultCenter] postNotificationName:QP_NotifacationName_SessionList_MoveTo_Next_UnReadMessage object:nil];
}
}
}
//判断是否进行了双击tabbar
- (BOOL)checkIsDoubleClick:(UIViewController *)viewController
{
static UIViewController *lastViewController = nil;
static NSTimeInterval lastClickTime = 0;
if (lastViewController != viewController) {
lastViewController = viewController;
lastClickTime = [NSDate timeIntervalSinceReferenceDate];
return NO;
}
NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
if (clickTime - lastClickTime > 0.5 ) {
lastClickTime = clickTime;
return NO;
}
lastClickTime = clickTime;
return YES;
}
三, 消息页面接受到通知后,进行后续滑动操作
如果只是一个列表则如下就可以
//通知方法的实现
- (void)doubleClickTabItemNotification{
NSLog(@"双击了 双击了 双击了 双击了");
ConversationListCell *firstCell = [self.tableView visibleCells].firstObject;
NSIndexPath *firstCellIndex = [self.tableView indexPathForCell:firstCell];
ConversationListCell *lastCell = [self.tableView visibleCells].lastObject;
NSIndexPath *lastCellIndex = [self.tableView indexPathForCell:lastCell];
if (firstCellIndex.row < self.dataArray.count-1 && lastCellIndex.row < self.dataArray.count-1) {
for (NSInteger i = firstCellIndex.row+1; i < self.dataArray.count; i++) {
EaseConversationModel *lastModel = self.dataArray[i];
if (lastModel.conversation.unreadMessagesCount>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
break;
}
}
}else{
for (NSInteger i = 0; i < self.dataArray.count; i++) {
EaseConversationModel *lastModel = self.dataArray[i];
if (lastModel.conversation.unreadMessagesCount>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
break;
}
}
}
}
如果只是一个列表中有两个数据源,并且含有headerView, 如
截屏2019-12-24下午3.14.47.png
则如下
- (void)doubleClickTabItemNotification{
NSLog(@"双击了 双击了 双击了 双击了");
HomeTableViewCell *firstCell = [self.tableView visibleCells].firstObject;
NSIndexPath *firstCellIndex = [self.tableView indexPathForCell:firstCell];
HomeTableViewCell *lastCell = [self.tableView visibleCells].lastObject;
NSIndexPath *lastCellIndex = [self.tableView indexPathForCell:lastCell];
//记录是否查询到结果
BOOL result = NO;
//判断置顶的数据
if (firstCellIndex.section == 0 && lastCellIndex.section == 0) {
if (self.dataArray.count) {
if (firstCellIndex.row < self.topDataArray.count && lastCellIndex.row < self.topDataArray.count) {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex lastCellIndex:lastCellIndex];
//如果没有查询到结果, 则进入未置顶数据进行查询, 并且此时是从0 开始查询
if (!result) {
result = [self queryDataArrayUnreadMessage];
}
//如果还没查到, 则在进入 置顶数据,已经滑出视图的数据中进行查找
if (!result) {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
}
else {
if (firstCellIndex.row < self.topDataArray.count-1 && lastCellIndex.row < self.topDataArray.count-1) {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex lastCellIndex:lastCellIndex];
//如果还没查到, 则在进入 置顶数据,已经滑出视图的数据中进行查找
if (!result) {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
else {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
}
else if (firstCellIndex.section == 0 && lastCellIndex.section == 1) {
//如果显示cell的起始位置小于置顶数据, 并且显示cell的结束位置也小于置顶数据, 则应该是先在置顶数据中查找
if (lastCellIndex.row < self.dataArray.count-1) {
//记录是否查询到结果
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex lastCellIndex:lastCellIndex];
//如果没有查询到结果, 则进入未置顶数据进行查询, 并且此时是从0 开始查询
if (!result) {
result = [self queryDataArrayUnreadMessage];
}
//如果还没查到, 则在进入 置顶数据,已经滑出视图的数据中进行查找
if (!result) {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
else {
result = [self queryTopDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
else if (firstCellIndex.section == 1 && lastCellIndex.section == 1) {
if (lastCellIndex.row < self.dataArray.count - 1) {
//记录是否查询到结果
result = [self queryDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex lastCellIndex:lastCellIndex];
//如果没有查询到结果, 则进入置顶数据进行查询, 并且此时是从0 开始查询, 如果查不到,则一直查到结束
if (!result) {
result = [self queryTopDataArrayUnreadMessage];
}
//如果还没查到, 则在进入 未置顶数据,已经滑出视图的数据中进行查找
if (!result) {
result = [self queryDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
else {
//如果没有查询到结果, 则进入置顶数据进行查询, 并且此时是从0 开始查询, 如果查不到,则一直查到结束
if (!result) {
result = [self queryTopDataArrayUnreadMessage];
}
//如果还没查到, 则在进入 未置顶数据,已经滑出视图的数据中进行查找
if (!result) {
result = [self queryDataArrayUnreadMessageWithFirstCellIndex:firstCellIndex];
}
}
}
}
//查询顶部数据 从显示的第一个cell位置的下一个位置开始查询 如果查不到则一直查询到顶部数据结束
- (BOOL)queryTopDataArrayUnreadMessageWithFirstCellIndex:(NSIndexPath *)firstCellIndex lastCellIndex:(NSIndexPath *)lastCellIndex{
//记录是否查询到结果
BOOL result = NO;
NSInteger startIndex = firstCellIndex.row + 1;
if (self.tableView.contentOffset.y < 70) {
startIndex = firstCellIndex.row;
}
for (NSInteger i = startIndex; i < self.topDataArray.count; i++) {
HomeNewsModel *lastModel = self.topDataArray[i];
if (lastModel.unReadMessageCout > 0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
//表示已经查询到结果
result = YES;
break;
}
}
return result;
}
//查询未置顶数据, 从数据的第一个位置0开始, 如果查不到, 以一直查询到数据结束
- (BOOL)queryDataArrayUnreadMessage{
//记录是否查询到结果
BOOL result = NO;
for (NSInteger i = 0; i < self.dataArray.count; i++) {
HomeNewsModel *lastModel = self.dataArray[i];
if (lastModel.unReadMessageCout>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:1];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
//表示已经查询到结果
result = YES;
break;
}
}
return result;
}
//查询顶部数据 从0位置开始查询 如果查不到则一直查询到显示的第一个cell位置的前一个
- (BOOL)queryTopDataArrayUnreadMessageWithFirstCellIndex:(NSIndexPath *)firstCellIndex{
//记录是否查询到结果
BOOL result = NO;
//如果还没查到, 则在进入 置顶数据,已经滑出视图的数据中进行查找
for (NSInteger i = 0; i < firstCellIndex.row; i++) {
HomeNewsModel *lastModel = self.topDataArray[i];
if (lastModel.unReadMessageCout>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
break;
}
}
return result;
}
//查询未置顶数据 从显示的第一个cell位置的下一个位置开始查询 如果查不到则一直查询到未置顶数据结束
- (BOOL)queryDataArrayUnreadMessageWithFirstCellIndex:(NSIndexPath *)firstCellIndex lastCellIndex:(NSIndexPath *)lastCellIndex{
//记录是否查询到结果
BOOL result = NO;
NSInteger startIndex = firstCellIndex.row + 1;
//70 为headerView的高度
if (self.tableView.contentOffset.y < 70) {
startIndex = firstCellIndex.row;
}
for (NSInteger i = startIndex; i < self.dataArray.count; i++) {
HomeNewsModel *lastModel = self.dataArray[i];
if (lastModel.unReadMessageCout > 0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:1];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
//表示已经查询到结果
result = YES;
break;
}
}
return result;
}
//查询置顶数据, 从数据的第一个位置0开始, 如果查不到, 以一直查询到数据结束
- (BOOL)queryTopDataArrayUnreadMessage{
//记录是否查询到结果
BOOL result = NO;
for (NSInteger i = 0; i < self.topDataArray.count; i++) {
HomeNewsModel *lastModel = self.topDataArray[i];
if (lastModel.unReadMessageCout>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
//表示已经查询到结果
result = YES;
break;
}
}
return result;
}
//查询未顶部数据 从0位置开始查询 如果查不到则一直查询到显示的第一个cell位置的前一个
- (BOOL)queryDataArrayUnreadMessageWithFirstCellIndex:(NSIndexPath *)firstCellIndex{
//记录是否查询到结果
BOOL result = NO;
for (NSInteger i = 0; i < firstCellIndex.row; i++) {
HomeNewsModel *lastModel = self.dataArray[i];
if (lastModel.unReadMessageCout>0) {
self.currentCellIndex = [NSIndexPath indexPathForRow:i inSection:1];
[self.tableView scrollToRowAtIndexPath:self.currentCellIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
break;
}
}
return result;
}
网友评论