-4

I have 2D array var students = [[Student]] I need to conform this array should be keep always 3 in memory so when this array insert new itme then remove from old from top

I have to insert array this way

self.students.append(student) //Update students  property

and trying with this line but tableview not update smoothly when I remove item using this line

self.students.removeFirst()

so what How can I remove array item from top for table view smoothly scrolling

Note array hooked table view

func numberOfSections(in tableView: UITableView) -> Int {
    return self.students.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return students[section].count
}

Update : enter image description here

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73

2 Answers2

2

With a fixed upper size, you would be better off not removing items at all.

Use a fixed-size array of three items, some of which could be empty, and a separate count and firstIndex variables. Effectively, your array becomes a Circular Queue:

var students : [[Student]] = [[], [], []]
var count = 0;
var firstIndex = 0;
// Adding a new item
students[(firstIndex+count) % students.count] = newItem
if (count != 3) {
    count++
} else {
    firstIndex = (firstIndex+1) % students.count
}
// Iterating the array
for var i in (0..<students.count) {
    let currentStudent = students[(firstIndex+i)%students.count]
    ...
}

The number of sections is count:

func numberOfSections(in tableView: UITableView) -> Int {
    return count
}

The only trick is to figure out which index to use for a given index path:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return students[(section + firstRow) % students.count].count
}

let actualIndex = (indexPath.section + firstIndex) % students.count
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you so much I am trying with your answer in my project and get back you – Nazmul Hasan Aug 01 '17 at 14:08
  • it is working fine but when trying to append 4th item to the a array then flowing Line `(firstIndex+count) % students.count` give me log `3` is `fatal error: Index out of range` – Nazmul Hasan Aug 01 '17 at 15:19
  • 1
    @NazmulHasan This tells me that `students.count` is above 3, which should never happen, because `students.count` is fixed at 3, and any positive number mod `%` 3 is 0, 1, or 2. `append` should be replaced with the circular addition code above. – Sergey Kalinichenko Aug 01 '17 at 15:26
  • thanks basically it is working fine from 1 to 3 but when adding 4th item to the array then tableView scrolling not goes down so not showing 4th item on the other hand log see array data updated . you can check my question update section image and sample code https://github.com/nazmulkp/PagginationSwift3 – Nazmul Hasan Aug 02 '17 at 19:00
  • please help me I am stuck all most 7 days – Nazmul Hasan Aug 02 '17 at 19:02
  • 1
    @NazmulHasan I thought you wanted to cap the number of elements to three. When you add element four, element one should disappear, right? If you want scrolling to continue fine, you should tell `UITableView` that you are deleting rows in the entire section zero by calling `deleteRowsAtIndexPaths`. After that you need to tell `UITableView` that you are inserting a bunch of rows at the third section (section index 2). This way you would be able to avoid reloading the whole table, disrupting the scroll. – Sergey Kalinichenko Aug 02 '17 at 19:12
  • Right ! I wanted to cap the number of elements to three. When you add element four, element one should disappear. but I spend all most 7 days . can not mange this . would help me please how can I do this . – Nazmul Hasan Aug 02 '17 at 19:15
  • @NazmulHasan Please try what I described in the comment (delete section zero, then insert section 2). – Sergey Kalinichenko Aug 02 '17 at 19:16
  • Thank you so much! I am trying with your described in the comment get back you – Nazmul Hasan Aug 02 '17 at 19:18
  • Sir I unable to resolve the error `'attempt to delete row 3 from section 2, but there are only 1 sections before the update'` when I trying to (delete section zero, then insert section 2) . please help me – Nazmul Hasan Aug 03 '17 at 14:25
  • Sir! here is the commit change . https://github.com/nazmulkp/PagginationSwift3/commit/0ce0f8cbf91c0cfd5969d8cdd4d340021817a9f5 – Nazmul Hasan Aug 03 '17 at 14:28
  • @NazmulHasan All deletions should be performed on zone zero. Don't forget to call `tableView.beginUpdates()` before and `tableView.endUpdates()` after performing the changes. – Sergey Kalinichenko Aug 03 '17 at 14:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150973/discussion-between-nazmul-hasan-and-dasblinkenlight). – Nazmul Hasan Aug 03 '17 at 14:30
  • Sir I did `tableView.beginUpdates(`) before and `tableView.endUpdates()` but it is not working for me – Nazmul Hasan Aug 03 '17 at 14:36
  • sir please help me i unable to figure out how to solve this problem – Nazmul Hasan Aug 03 '17 at 18:17
1

Use students.removeFirst()for removing the first element and students.removeLast() for removing the last element.

If you want to remove it at a certain index use, students.remove(at: theindexyouwant)

EDIT:

If you want animation during the delete use: tableview.deleteRowsAtIndexPaths(tRemove, withRowAnimation: .Left)

João Fernandes
  • 491
  • 7
  • 17