-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

10
  • stackoverflow.com/questions/24051633/… ? Commented Aug 1, 2017 at 13:45
  • Possible duplicate of How to remove an element from an array in Swift Commented Aug 1, 2017 at 13:46
  • @Larme tableview not update smoothly when I remove item using self.students.removeFirst() Commented Aug 1, 2017 at 13:47
  • @DávidPásztor tableview not update smoothly when I remove item using self.students.removeFirst() Commented Aug 1, 2017 at 13:50
  • 2
    @NazmulHasan then why didn't you ask about smooth updating? Without editing your question to reflect your actual problem, it is a duplicate. Commented Aug 1, 2017 at 13:51

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

15 Comments

Thank you so much I am trying with your answer in my project and get back you
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
@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.
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 github.com/nazmulkp/PagginationSwift3
@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.
|
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)

2 Comments

tableview not update smoothly when I remove item using self.students.removeFirst()
tableview.deleteRowsAtIndexPaths should not work because it is 2D array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.