What order do batched row insertions/deletions take place in UITableView? - ios

UITableView allows you to batch editing operations using beginUpdates and endUpdates.
My question is: do I need to know whether it does deletions or insertions first? Or can I refer to everything by the index path prior to beginUpdates and it'll magically work?
Suppose I have a table:
A (currently index path 0,0)
B (0,1)
C (0,2)
D (0,3)
E (0,4)
F (0,5)
I want to turn it into:
A (0,0)
C (0,1)
D (0,2)
H (0,3)
E (0,4)
F (0,5)
Thus, I've deleted B (which was at 0,1) and inserted H (which was inserted after D — at 0,4 before the deletions, or 0,3 after).
So, between my begin/end updates calls, which of these will work?
deleteRowsAtIndexPaths: 0,1, followed by insertRowsAtIndexPaths:
0,4
deleteRowsAtIndexPaths: 0,1, followed by
insertRowsAtIndexPaths: 0,3
insertRowsAtIndexPaths: 0,4, followed by deleteRowsAtIndexPaths: 0,1
insertRowsAtIndexPaths: 0,3, followed by deleteRowsAtIndexPaths: 0,1

The relevant Apple documentation for this is under Ordering of Operations and Index Paths.
Deletion and reloading operations within an animation block specify which rows and sections in the original table should be removed or reloaded; insertions specify which rows and sections should be added to the resulting table. The index paths used to identify sections and rows follow this model.
So the table view will first perform any delete or update operations, whose index paths refer to index paths in the original table contents. Then insertions are performed, and those index paths refer to index paths after deletions have occured.
So in theory your number '2' option should be the one you want.

Related

how could I count minus cells in a column excluding the last one?

I want to count on another sheet that how many people have a minus balance. Of course, I should use COUNTIF, but how could I dynamically narrow the range from C2 to the second last non-blank cell(because there's a chance for the total balance to be minus, it should be excluded)?
Try below formula-
=COUNTIFS(C2:INDEX(C2:C,COUNTA(C2:C)-1),"<0")
Here C2:INDEX(C2:C,COUNTA(C2:C)-1) will return a array of values as well cell reference from C2 to last second non empty cell in column C (Assume you do not have any blank rows inside data). If you have blank row, then you have to use different approach. See this post by #TheMaster
COUNTA(C2:C) will count all non blank cells in C column then minus 1 from that count to make it second non empty cell till down to up.
And then apply COUNTIFS() function.

How to use ARRAYFORMULA with OFFSET to previous row without getting circular reference error

Example sheet: https://docs.google.com/spreadsheets/d/14ma-y3esh1S_EkzHpFBvLb0GzDZZiDsSVXFktH3Rr_E/edit?usp=sharing
In column B of ItemData sheet, I have achieved the result I want by copying the formula into every cell in the column, but I want to solve this using ArrayFormula instead.
In column C I have achieved the same result using ArrayFormula. However, for addition, column C is referring to cells in column B, while column B is referring to cells in column B. I.e. every cell in column B is adding 1 to the cell on the row above.
If I select the C3 formula text and paste it into the cell edit field for cell B3 (to not screw up cell references during copy - I know I could make them static references, but this is not my problem), the cell gets an error value of
#REF!
Error
Circular dependency detected. To resolve with iterative calculation, see File > Spreadsheet Settings.
Do note that the additions that need to be done are the same in both cases: Add 1 to the value of the cell on the previous row, so there is no circular reference involved. There is a starting value provided in B2, and cells in B3 and downwards should use the data from the B cell in the previous row.
Also, note that I did try File->Spreadsheet settings and enabling circular reference computation with max 25 items, but this only fills in the first two cells (B3 and B4).
How can I solve this problem? I would prefer having something like ArrayFormula, where the formula only exists in a single cell. But copy-pasting would be acceptable as long as any new rows, inserted in between or added at the bottom, would get the same formula added in column B.
Will matching items always be consecutive? It seems that way since you're comparing each Item cell to the cell above it right in your formula logic. That breaks an [unwritten?] rule of spreadsheet normalization; values' addresses themselves generally should not be treated as data.
IF you're committed to it though, have you considered explicitly using location as a data source? Example:
=ARRAYFORMULA(IFS(
NOT(LEN(A3:A40)),,
ROW(A3:A40)-3-MATCH(A3:A40,A$3:A$40,0)<=VLOOKUP(VLOOKUP(A3:A40,Items!$A$2:$D,2,false),DataPerColor!$A$2:$B,2,false),ROW(A3:A40)-3-MATCH(A3:A40,A$3:A$40,0),
true,
))
Just like your formulas, all that does in English is:
for each row,
if there's no Item, don't output any ItemData,
if the number that belongs in this cell¹ is less than or equal to the lookup, print it,
otherwise, don't output any ItemData
But then what is ¹ "the number that belongs in this cell" and how can we calculate it without using column B? I abuse locations of things to get it. Looking down your row B, each number that appears is just:
this row's number,  minus  
the row where items start [always 3],  minus  
the row number [in just the Item rows] of the first row containing this row's Item
Using the second-to-last ItemC as an example: the first ItemC is the 16th item listing, and the one we're looking up… the "second-to-last ItemC" is in row 21 of the sheet. 21-3-16 = 2 …the number you wanted.
If you can stomach that, it's a single formula and does work according to your specifications.

Array logic with indexpathforselectedrow

So I’m attempting to grasp nested array logic using indexpathforselectedrow!
therefore, ill be using examples to explain my confusion.
my first thought: lets say I have
var colors: array = [“red”,”orange”,”yellow”]
in a table view.
If i got the indexpathforselectedrow of orange, what would that index value be? I assume that would be section = 0, row = 1, so [0][1]. would that be correct? or since this is a simple array, would the value just be one value? 1?
Secondly, if I have a more advanced array (please let me know if this array setup is correct)
var morecolor: [[“green”,”blue”,”teal”][“light green”, “dark green”]
[“light blue”, “dark blue”][“light teal”, “dark teal”]]
if I selected green (first array/ array 0) and wanted to segue to the light/dark green array, the second array (or first), how would i go about this?
An index path (NSIndexPath) consists of a section and a row. Both are integers, zero-based.
The first section in your table is section 0, the second is 1, etc. Same for rows.
You implement numberOfSections to tell how many sections you want, and numberOfRowsInSection to tell how many rows in each section (that method passes you the section number it needs the number of rows for).
Typically you have a model that represents the data to be displayed, and you index into that model in cellForRowAtIndexPath.
If you have a one-dimensional model, say a simple list of colors, you might have just one section, and as many rows as colors in your model. The color corresponding to any row would be something like:
model.colors[indexPath.row]
You can ignore section here because you know it's always zero (you only have 1 section).
If you have a two-dimensional model, say a set of themes each of which consists of several colors, you would have as many sections as themes, and rows as colors in each theme. The theme and color corresponding to any row would be something like:
model.theme[indexPath.section].colors[indexPath.row]
Many table view methods use index paths, so it's useful to know how they work.
T0 answer your first question, yes you're correct in saying that such a call would return an index path for item at section 0, row 0, but in actuality since it's an index path and not a regular integer representation the rows aren't represented as 1 2 3 4... etc but rather 0 64 128 etc... Thus, in order to get the row that was returned you can access it using indexPath.row which returns the int representation of the row selected.

How to map vector of elements into UICollectionView grid?

I have a 1D vector of images (something like 3000) that I want to map into a UICollectionView. I am having trouble implementing the delegate method cellForItemAtIndexPath -- namely, how do I deal with NSIndex? I've looked at all the documentation and tutorials on the web, and everybody glosses over this detail: How do I map from this foreign object NSIndex (I don't even know where this comes from) into a single integer index for my array?
As for how the grid should look, one of the following:
(A) Have a preset number X rows of images, with the ability to scroll horizontally by swiping to see the collection. Ordering should be as such:
1 X+1 2X+1 ...scroll
2 X+2 3X+2 ...scroll
.
.
X X+X 2X+X ...scroll
(B) Have a preset number Y columns of images, with the ability to scroll vertically by swiping to see the collection:
1 2 3 ... Y
Y+1 Y+2 Y+3 ... Y+Y
.
.
scroll up and down
.
.
Do I need dequeueReusableCell of a custom kind? Subclass my own FlowLayout? I'm confused because this NSIndex is so baffling. It's supposed to be a bunch of concatenated integers, it seems, but my data source is a 1D array and I have no idea how to extract the index from NSIndex as the tutorials all use something like assetsArray or 2D array, and documentation of NSIndex just shows a bunch of arrays, not how to use it with a single array.
Thanks!
Ok the NSIndexPath from the UICollectionViewDatasouce protocol is just a Section and Row pair, Not a grid. Have you implemented the datasource methods collection:numberOfItemsInSecion and numberOfSectionsInCollection. You must do these first. The deal with cellsForItemAtIndexPath.
So for example in your case, *numberOfItemsInCollection would return 1. This means that there is only 1 section of data. Then *collection:numberOfItemsInSection would return the count of your 1d array. The indexPath object has a section and row property. So if your numberOfItemsInCollection return 4 (meaning there is 4 sections of data), then the collection:NumberOfItemsInSection would be called 4 at least 4 times. Each time, the indexPath.section would tell you what section of the data does the collectionView need a count for.
In collection:cellForItemAtIndexPath: datasource delegate, will be call at least for every item in your 1d array. The indexPath.section will tell you what section of data and indexPath.row will tell you want item. In your case since you should only have 1 section, indexPath.row will be the index into your 1d array. ( myarray.objectAt(indexPath.row or myarray[indexPath.row] )
This Datasource protocol is exactly like UITableViewDatasource.
Once more thing, the UICollectionView has nothing to do with how the grid of items are laid out. The Layout object does that for you. If you want a grid of items then use FlowLayout.

Follow formula patterns for some cells only

I am trying to get excel to follow patterns grabbing information from cells only, while keeping other cells in the formula static.
As follows:
=importxml(CONCATENATE(A1,B17),E1)
A1 = never changes
B17 = increments by 1 every time. Ex: B17, B18, B19, etc...
E1 = never changes
Right now it seems like excel is counting the number of cells from the cell I am currently in and once I try to follow that pattern it keeps counting the same number of cells to grab the cell in the pattern. (Ex: 7 cells up, so as I move down from the current cell it keeps looking for data in the cell 7 cells up from the current one, which is often, empty). Moreover, right now all cells are changing in the pattern and I only need SOME cells to follow the pattern.
I appreciate any help with this.
If I understand your question, you want to copy that formula while keeping the first and last parameters (A1 and E1) constant. That can be achieved by using the $ sign:
$A$1 will always be copied as $A$1
$A1 will be copied as $An where n is the row number - so if you copy it one row down (whatever the column) it will become $A2
A$1 will be copied as XX$1 where XX is the column - so if you copy it one column to the right, (whatever the row) it will become B$1
So in your case you would use:
=importxml(CONCATENATE($A$1,B17),$E$1)
or if you need to make the B column constant (but not the row):
=importxml(CONCATENATE($A$1,$B17),$E$1)

Resources