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.
Related
If I have hierarchical data in a Google Sheet as in columns A and B in the example below, how can I write a formula that will fill the corresponding cells in column C with the product of the "parent" value in A1, and the "child" values in column B. That is, The formula in C8 for example, will search upward in column A until it finds the value 5 in A6, then multiplies it by 8, the value in B8.
Obviously I'm trying to avoid having to put the "parent" value in every row in column A.
After a fiddling around and jogging my memory on ArrayFormulas I figured out how to get the result I wanted. Here's the formula I'm using:
=ArrayFormula(index($B$2:$B2,MAX(IF(ISNUMBER($B$2:$B2),ROW($B$2:$B2)))-1,1))
(Note: Row 1 contains headings)
Edit: Explanation of how I arrived at this solution:
Ordinarily, IF(ISNUMBER(cell)), and ROW(cell) would return TRUE/FALSE, or a row number respectively. When used in an array formula and a range of cells as input instead, what you get is a list of TRUE/FALSE values, and row numbers evaluated for each cell in the input range. Wrap MAX around that and it will return the position in those two lists where IF(ISNUMBER()) is TRUE and the row number returned by ROW() is the highest, effectively searching from the bottom of the range.
The absolute reference for the first element in the range to be searched (in this case $B$2) keeps the range anchored to the same starting location when dragging the formula into other cells, while the lower bound of the range (in this case $B2) to be searched grows vertically.
Finally, the INDEX takes the input range, row number returned by the above (-1 row because of the header) and column position (1, since there is only one column) in order to return the desired value.
One way you could do this would be to associate each value in column B with a category or key that can be used to lookup the value for A in a separate table. This abstracts it somewhat, so you can change the values for the A column without having to have them in every row, but there's no empty cells.
i.e.
and lookup table:
In column C
= VLOOKUP($A1, <range for category lookup table>, 2, 0) * $B1
(And then this formula can be filled down)
More on VLOOKUP in Google Sheets here
Alternatively I suppose you could use a formula to find the last non-empty row in column A, or something along those lines, but this is more of a hack than a proper way to structure your data. Tables aren't really designed to be used in a hierarchical fashion like what you've shown. But they can easily represent hierarchical data using techniques like what I've suggested.
I'm unable to get OFFSET to return an array of data in Google Sheets. For example in the same sheet, I have a player table which eats an animal, the eats column stores the index of the animal. I want to add up all weights.
This is what I came up with (the example numbers might not be exactly right) but it only works with the first split value:
=SUM(IFERROR(OFFSET($H$11,SPLIT(D6,","),0,1,1),0))
Assuming User A is in B2 and your other table is a Named Range (1), please try:
=ArrayFormula(sum(vlookup(split(C2,","),NamedRange1,3,0)))
I have a a couple sheets that I want to filter the values of and sum them together. I have a working function:
=SUM(INDEX(Grades!M32:V32,0,B2)+INDEX(Grades!M32:V32,0,C2)+INDEX(Grades!M32:V32,0,D2)+INDEX(Grades!M32:V32,0,E2)+INDEX(Grades!M32:V32,0,F2)+INDEX(Grades!M32:V32,0,G2))
and this is working fine. The problem is it isn't really dynamic and I was wondering if there was a more efficient way to approach this instead of using so many Indexes.
Index is grabbing a horizontal range of numbers from another sheet and getting the needed index position from a small table in another sheet.
Update
I was able to get the formula working how I wanted by inserting an IF statement that would not do the VLOOKUP if one of the cells was empty.
=ArrayFormula(SUM(IF(B2:I2 <> "",VLOOKUP(1,{1,Grades!$M$32:$V$32},B2:I2+1))))
It looks a bit strange, but this is one way:
=ArrayFormula(SUM(VLOOKUP(1,{1,Grades!M32:V32},B2:G2+1,0)))
In the VLOOKUP, 1 is the value you are searching for, {1,Grades!M32:V32} is the array in which you are searching, B2:G2+1 is the column index (or rather, indices) you wish to return, and 0 dictates that you require an exact match.
{1,Grades!M32:V32} constructs a horizontal, one-row array that has 1 in the left-most element, with the values in Grades!M32:V32 to the right of it.
VLOOKUP will search down the left-most column of that array. It will always "find" the 1 that it is searching for in the left-most column, because we have purposely manufactured that.
Where the action really happens is in the third argument, where we return the columns in that manufactured array corresponding with the values in B2:G2 (I should add, I assumed that all cells in B2:G2 are populated - it will return the wrong result if any are blank). The +1 is to account for the extra element (the 1) we tacked on to the left of the lookup array.
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.
I HAVE THIS: A tableview with cells with a label were user gets a number. (From core data from a detail view).
WHAT I WANT: How to make an operation with all the cells with a number on a label on that cell.
Example, sum up all the cells label. label at cell 1, + label at cell 2, and so on.
MY PROBLEM: i can do it but with a lot of code, i have to make an operation for each cell user may create. (Maybe user creates 1 or 20 cells), and i think to make an operation with the index path and value for 20 cells its not necessary.
Im sure its another way but i don't know how.
Thanks from know to everybody.
So, you have a couple of options, this one is little code but isn't so efficient as the numbers start to add up:
NSArray *myNumbers = ...;
I'm assuming that this array exists and is being used to drive your table view. It contains the instances of your Numbers entity. Based on that, you can get the sum:
NSNumber *sum = [myNumbers valueForKeyPath:#"#sum.mynumber"];
The alternate route is to use NSExpression and NSFetchRequest to run the summing operation on the entities in the data store without actually reading everything into memory like the array does.
There is also a middle ground option where you could just run an NSFetchRequest to get only the mynumber values in dictionaries and then use valueForKeyPath:. The value of this option depends on how many attributes your Numbers entity has and what they contain.