last day (14 days later) » 

15:31
1
A: Number of rows in section error Swift

NishantTry this code for inserting rows: func insertData(){ let newObjects = linesmain[answertitle1] as! NSArray // appending the array to increase count //someTagsArray += newObjects someTagsArray.addObjectsFromArray(newObjects) self.tableView!.beginUpdates() var i...

returns the following error The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (4 inserted, 0 deleted)
I have edited the code a little bit. Please check now..
just checked checked - unfortunately, binary operator += cannot be applied to NSArray type of objects.
checked again. array doesn't have a member named addObjectsFromArrays. wasn't it deprecated in swift 1.2 ?
You can use someTagsArray.addObjectsFromArray(newObjects) instead. OR you can change the datatype for newObjects to Array in order to use the binary operator (+=).
thank you. changed the type from NSArray to simple Array and the appending worked! but the issue is - it loads inside the already loaded rows. i want it to load after the last row - at the bottom.
15:31
change the for loop to this for var i = 0; i < newObjects.count; ++i { insertedIndexPaths.addObject(NSIndexPath(forRow: someTagsArray.count+i, inSection: 0)) }
unfortunately, it crashes ( reason: attempt to insert row 7 into section 0, but there are only 7 rows in section 0 after the update
insertedIndexPaths.addObject(NSIndexPath(forRow: someTagsArray.count+i-1, inSection: 0))
try this
hi
checking
still crashing
i even tried replacing it to -2
whats the crash now?
absolutely the same
reason: 'attempt to insert row 7 into section 0, but there are only 7 rows in section 0 after the update'
hi u there?
15:46
yes
sorry a bit busy in some work
what is the initial and final number of rows that should be in the tableview?
sure i understand
checking the numbers
initially its return someTagsArray.count
yes, whats the value of this count?
and what should be the final count? after inserting.
the final number should be 8
15:51
so the max row index should be 7
yup i believe so
in that case it should not crash!!
@DavidRobertson It says there are only 7 rows in section 0
i'm sort of a lost here.
please send me your func insertData() here
sec
func insertData(){

let newObjects = linesmain[answertitle1]! as Array

println(linesmain)
println(answersdict)


someTagsArray += newObjects

self.tableView!.beginUpdates()

var insertedIndexPaths: NSMutableArray = []

for var i = 0; i < newObjects.count; ++i {
insertedIndexPaths.addObject(NSIndexPath(forRow: someTagsArray.count+i-2, inSection: 0)) }

self.tableView?.insertRowsAtIndexPaths(insertedIndexPaths as [AnyObject], withRowAnimation: .Fade)

self.tableView!.endUpdates()

self.tableView.reloadData()
15:57
initially, someTagsArray.count == 3 ?
what is newObjects.count? plz check.
checking
yes, someTagsArray.count should be equal to 3
newObjects.count is also equal to 3
ADDITIONAL NOTE: self.tableView.reloadData() writing this will not allow the Fade animation to be visible, as it will reload the table immediately.
ok so final row count should be 6
oh ok got the issue.
let me write the code here...
i'm adding reloadData to update the uibutton labels in the second nib
thank you for noting, anyway
Basically, if u have to reload, u dont have to go through the hassles of inserting the rows. just add to array and reload. :)
i tried simple someTagsArray.append(element) but the problem is - it doesn't load elements after the last row (and the last row is a different array and a different nib)
16:09
func insertData(){

let newObjects = linesmain[answertitle1]! as Array

println(linesmain)
println(answersdict)

let initialCount = someTagsArray.count as Int

someTagsArray += newObjects

self.tableView!.beginUpdates()

var insertedIndexPaths: NSMutableArray = []

for var i = 0; i < newObjects.count; ++i {
insertedIndexPaths.addObject(NSIndexPath(forRow: initialCount +i, inSection: 0)) }

self.tableView?.insertRowsAtIndexPaths(insertedIndexPaths as [AnyObject], withRowAnimation: .Fade)

self.tableView!.endUpdates()
plz try this code
and lets try it without reloadData :)
sure)
app starts yet when i initiate the function nothing happens
in which method are you calling this function?
make sure the arrays are populated properly
same method as before
just changed the code inside it
16:15
i meant insertData() function?
I'm calling it from the observer
like this
NSNotificationCenter.defaultCenter().addObserver(self, selector: "insertData", name: "buttonClickedNotification", object: nil)
the original sender action happens in a separate nib class
so insertData() is being called properly but its not working?
yes
it doesn't populate data
16:17
please make sure that newObjects.count is not zero.
checking that now
atleast its not crashing :)
yes) thats a relief
i don't know what i just did
just re-copied the code
and it worked
now i somehow have to populate data to the uibutton title in the second nib )
tagging your answer as the solution in the forum
great
you were very helpful thank you
16:23
no problem bro!
actually using two custom cell nibs in the tableview is a pain in the ass)
with almost no decent tutorials
not if you are storyboards
well, i've created them via storyboards
i have had 4 prototype cells in a single table
i know its easy when you do something like
if(indexPath[row] == 1){
nib1
}else {
nib2
16:25
no i dont. i use cellIdentifier that i have to set in the storyboard table view cell prototype
yes i do same stuff
and then assign this identifier to each indexPath.row - in the cellForRowAtIndexPath method
oh
You are from the States?
actually, I'm from Russia originally
this is just my nickname
okays... I am from India
there are some great devs in India, i personally worked with two teams
not very good in terms of design, but very strong engineers
My real name is Michael, nice to meet you
16:30
yes you can say :)
same here Michael..
we can connect via linkedIn
thats a great idea
my profile name is Michael Shcheglov
actually, i'm testing the code now - the nibs with newObject are appended, but the nibs that contained uibutton - if i scroll are replaced with newObject
quite a weird thing
https://in.linkedin.com/pub/nishant-anthwal/32/470/580
share a screenshot please
give me a second
i believe the problem here is not the insertRowsAtIndexPath but rather my cellForRowAtIndexPath method (
this is rather tricky
16:50
this is happening bcuz the cell is being reused on scrolling.
you will have to keep track of the state in which the row is to be displayed. (question or answer)
u will have to used a global array or something to store the states for each indexPath..
thanks i will give it a shot
do you have a skype
yes
nishant.anthwal

last day (14 days later) »