我有一个tableView,可以一次显示5个单元格。我的要求是根据用户的操作向上或向下滚动时显示前5个或后5个。我做了一些代码,但没有达到要求,所以请随时让我进一步了解相同的内容,并建议我以更好的方式进行处理。
谢谢
我的代码
func getNotifs(_ isTopPos: Bool) -> MyNotifications {
let coolIndex = self.lastIndex
if isTopPos == false {
let tempLastIndex = coolIndex + 5
if self.lastIndex == 0 {
if tempLastIndex < self.getAllNotificationsCount() {
self.lastIndex = 0
return Array(self.allNotifications[coolIndex..<self.getAllNotificationsCount()])
}
self.lastIndex = tempLastIndex
return Array(self.allNotifications[coolIndex..<tempLastIndex])
}
if tempLastIndex >= getAllNotificationsCount() {
let tempIndex = getAllNotificationsCount() - 5
self.lastIndex = tempIndex
return Array(self.allNotifications[tempIndex..<getAllNotificationsCount()])
}
self.lastIndex = tempLastIndex
return Array(self.allNotifications[coolIndex..<tempLastIndex])
}
var tempLastIndex = coolIndex
tempLastIndex = tempLastIndex - 5
if tempLastIndex <= 5 {
self.lastIndex = 0
if (self.getAllNotificationsCount() < 5) {
return Array(self.allNotifications[0..<self.getAllNotificationsCount()])
}
return Array(self.allNotifications[0..<5])
}
self.lastIndex = tempLastIndex
return Array(self.allNotifications[tempLastIndex..<coolIndex])
}