willDisplaycell and cellForItemAt of collectionview have diffrent value - swift

I got data in a form of array from server at viewDidLoad
want to show it into collectionView according to its indexPath .
I have a collectionView but its cells are full length of collectionView frame so that cells work as a scrolling pages. at button click, next cell will show with its data array index values.
Lets assume data array are 3. First time its shows correct data array but after button click event in place of caliing cellForRowAt method single time , it call 2 times but at willDisplayCell method it run only one time. due to which the values of willDisplay and cellForRawAt are diffrent.
It occur when collectionView scroll
i except the flow of collectionView as
cellForItemAt Optional(“value of array index 1 ”) willDisplay
Optional(“value of array index 1 ”) UIDisplay of 1 array index
cellForItemAt Optional(“value of array index 2 ”) willDisplay
Optional(“value of array index 2 ”) UIDisplay of 3 array index
cellForItemAt Optional(“value of array index 3 ”) willDisplay
Optional(“value of array index 3 ”) UIDisplay of 3 array index
but i get
cellForItemAt Optional(“value of array index 1 ”) willDisplay
Optional(“value of array index 1 ”) UIDisplay of 1 array index
cellForItemAt Optional(“value of array index 2 ”) willDisplay
Optional(“value of array index 2 ”)
cellForItemAt Optional(“value of array index 3 ”) UIDisplay of 3 array
index

Related

How do I add numbers to each tableview row beginning from 1

I have created a TableView with a Prototype Cell with a lot of content in it, multiple labels, 2 pictures and a view which is colored if the row is selected.
What I want to do now, is I want to add a number for each of the rows created so the user can see how many rows there are. I can't use the ID I get from the Database since it is a random mix of characters.
I added an Int variable 'participantID' which starts from one and increments by one with every cell created and writes the value in a label within the prototype cell.
This does exactly what I want, with just one issue: Every time i scroll in the table the ID is incremented. I know that's because of the reuse of the cells however I couldn't find a way to fix this issue.
This is how it looks (particID):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tbcCourseParticipant", for: indexPath) as! participantCell
let partic = participants[indexPath.row]
cell.lblId?.text = String(particID)
cell.lblName?.text = partic.name
cell.lblFirstName?.text = partic.firstName
cell.lblBirthDate?.text = partic.birthDate
cell.lblPhoneNr?.text = partic.phoneNr
cell.lblPrice?.text = String(partic.payed) + "€"
particID += 1
return(cell)
}
The total number of rows is participants.count. The number of this row is indexPath.row+1 (I add 1 because the user will not expect to start counting at zero the way a programmer would do).
So I would say:
cell.lblId?.text = String(indexPath.row+1)
Or maybe:
cell.lblId?.text = String(indexPath.row+1) + " of " + String(participants.count)

Swift - My collection view is not outputting an extra amount

The count of chosenPlanArray.count is 5 here, and that is how many cells my collection view ends up having.
However, when i try an append values from the collection view to arrays, my collection view is iterating over itself an extra amount of times - in this example it gets to 9 iterations. Even though 'return chosenPlayArray.count' is 5. This is very random and ive been playing with my code all day trying to fix it. Has this ever happened to anybody ?
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return chosenPlanArray.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomPlanItCollectionViewCell
cell.planImage.downloadedFrom(link: chosenPlanArray[indexPath.item].imageForPlan!)
cell.planLbl.text = chosenPlanArray[indexPath.item].nameOfEvent
cell.dateTimeLbl.text = chosenPlanArray[indexPath.item].eventStartsAt
if cell.dateTimeLbl.text == nil {
datesForFirebase = [""]
} else {
datesForFirebase.append(cell.dateTimeLbl.text!)
}
individualPlanNames.append(cell.planLbl.text!)
imagesOfPlan.append(chosenPlanArray[indexPath.row].imageForPlan!)
print(chosenPlanArray.count)
print(imagesOfPlan.count)
print(nameOfEventPlan.count)
print(individualPlanNames.count)
return cell
}
Here is my output for those print statements at the bottom. Notice how the first print statment is correct - the actual size of the collection view is 5, as you can tell by the numberOfitems in sec.. I find it weird how the other print statements start at 1, and then loop past the size of 'chosenPlanArray.count' - Is the issue the way i may be appending the values to my arrays? This is causing me to get the wrong number of values uploaded to my firebase. So i need to figure this tricky situation out. Thanks
5 // first print statement print(chosenPlanArray.count)
1 // second print statement print(imagesOfPlan.count)
1 // fourth print(nameOfEventPlan.count)
1 // third print(individualPlanNames.count)
5
2
2
2
5
3
3
3
5
4
4
4
5
5
5
5
5
6
6
6
5
7
7
7
5
8
8
8
5
9
9
9
You have to know that there is cell reusing , which means every scroll a call to cellForItemAt happens which means these lines run again
individualPlanNames.append(cell.planLbl.text!)
imagesOfPlan.append(chosenPlanArray[indexPath.row].imageForPlan!)
and the size of their content increases

Section index in tableview without section (Swift)

Question 1: How can i have section index in tableview without section?
Question 2: Could i have those section index for specific rows in a table? For example: i want the section index to be implemented whenever indexpath.row > 4.
Table views in UIKit always have sections and rows. By saying you don't want a section, you are really saying that you simply want only one section (at section index 0) that holds all rows (with indexes relative to section index 0).
Note: You can easily have a section without a section header in which case the user will never know that all rows are actually contained in a section.
When you implement your table view data source, you will want to implement numberOfSections and numberOfRowsInSection in order to let the table view know how many sections and rows you want.
See UITableView.
I think you mean with only one single section (you can't have no sections). Here's how I do this:
Create index titles that are just string versions of the array indexes. If there are too many to fit on the screen, iOS will skip some and replace them with bullets to make it clear that there are some indexes not being displayed. You will be guaranteed that the index range is the same as your row range. (Unless you have zero rows).
Instead of returning the section index, return -1. This will do nothing. Programatically scroll to the ROW of the passed in index.
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if myDataSourceArray.count > 0 {
return Array(1...myDataSourceArray.count).map({String($0)}).map({ String($0) })
}
return []
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .middle, animated: true)
return -1
}
(I actually set my minimum count to 20, rather than zero, as there's not much point in having an index when all items in the table are on the screen. But you do need to check for at least greater than zero, otherwise it will try to create an index of "1" for a table with no rows, and this will cause a crash.)
You may want different index titles other than just numbers, but hopefully this gives you some idea on how it can be done. Once you have a different number of indexes to what you have rows, you would have to come up with some other more complicated way of relating index numbers or index titles to row numbers.
Question1:To have section index in tableview without section, you can make height of section to 0.
Question2: If indexPath.row>4, get section index as: yourArray[indexPath.section]

Add unknown number of elements to another cell array from a certain index on

I've data in the format of
a{1}(1,1)=1
a{1}(1,2)=3
a{1}(1,3)=0.5
a{1}(2,1)=1
a{1}(2,2)=5
a{1}(3,1)=2
a{1}(3,2)=7
...
Now I'd like to place item 1 & 2 of all rows in this cell into another cell array from an specific index on. How should I do this? for example to place column 1 & 2 of these unknown number of row in the cell array 'b' from index 5 on without repetition I used:
b{1}(1,(5:end + 1)) = unique(a{1}(:,(1:2)))
I'd like the output of cell array 'b{1}' from 5th elements becomes '1 2 3 5 7'.
However, it is false and gives the "Subscripted assignment dimension mismatch." error. I dont know where is the problem. Is my snippet right?
Any help is appreciated

iPhone UITableViewController move only specific rows in a table

I have implemented a standard UITableViewController. I have made only some rows in the table to be moveable, ie. canMoveRowAtIndexPath returns true only for some indexPaths (for rows in the first part of the table). I would like to allow exchange of only those rows that are moveable, ie. for which canMoveRowAtIndexPath returns true.
Eg. my table looks like this:
Row 1
Row 2
Row 3
Row 4
Row 5
Rows 1, 2, 3 are moveable. So I want to implement the following behavior: row 1 can be exchanged only with rows 2 or 3. Similary, row 2 can be exchanged only with rows 1 and 3.
This is one possible layout:
Row 3
Row 1
Row 2
Row 4
Row 5
However, I don't want this to happen:
Row 3
Row 5
Row 2
Row 4
Row 1
How to achieve this?
Keep in mind that it's actually only moving one row, not exchanging.
What you want is to implement tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath
See How to limit UITableView row reordering to a section
You can control the movement within section and between section.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// Do not allow any movement between section
if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
return sourceIndexPath;
// You can even control the movement of specific row within a section. e.g last row in a Section
// Check if we have selected the last row in section
if ( sourceIndexPath.row < sourceIndexPath.length) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
// you can use this approach or logic to check the index of the rows in section and return either sourceIndexPath or targetIndexPath
}