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.
Related
I’m trying to emulate Minesweeper in Google Sheets, and for this I want to create a second map adjacent to the first with all of the correct values already in it. To randomize bomb position, I need a list of random numbers or cells(cells would be preferable). However, I cannot figure out how to do this without ending up repeating numbers. The result would ideally be a vertical array of cell coordinates. Thank you!
Answer
The following formula should produce the result you desire:
=SORTN(FLATTEN(MAKEARRAY(10,10,LAMBDA(row,col,ADDRESS(row,col)))),20,,RANDARRAY(100),)
In =MAKEARRAY, change the first 10 to adjust how many rows to randomly choose from, or the second 10 to adjust how many columns to choose from. The value in =RANDARRAY must be equal to the product of the number of rows and the number of columns. (e.g. in the above example, 10*10=100).
Change the 20 to adjust how many randomly chosen values to return.
Explanation
=MAKEARRAY is used to generate an array of every possible row and column combination. It accepts a =LAMBDA, which in this case is just the =ADDRESS function. The first two arguments of =MAKEARRAY determine how large the array should be, which is why changing them adjusts how many rows/columns to randomly pick from.
Then, the result of =MAKEARRAY is squashed into a single column using the =FLATTEN formula.
Finally, the entire thing is sorted randomly using =SORTN combined with =RANDARRAY. =SORTN also limits the number of results that are returned dependent on its second argument, which is why changing it adjusts how many results are returned.
If you want information on how to "freeze" the value of =RANDARRAY so it doesn't recalculate each time you change something, check out this question by player0.
Functions used:
=MAKEARRAY
=LAMBDA
=ADDRESS
=FLATTEN
=SORTN
=RANDARRAY
I have a dataset, consisting in a calendar with multiple values defined by a key. It goes like this:
I want to take the existing data and put it in a set of columns so I can make a dispersion graphic to see the tendency of the values. This is the format that I'm talking about:
What I specifically need is for the value on the cell D20 from the first dataset (which will be a timestamp from January 22nd, tracked with the key "HORA" in the cell C20) to appear in the cell B23 of the other dataset (which is the cell tracking the time at the January 22nd).
I've tried with all the lookup functions, hoping that VLOOKUP and HLOOKUP could return me an array of values that have the desired key (aka finding the key and returning cell 2), and even queries (with which I still have some issues understanding).
Thank you so much!
That kind of organization of data is not very efficient for applying formulas, but obviously it is visually nice. What I can think of is trying to "offset" with XLOOKUP:
=XLOOKUP(A2,FLATTEN(MUESTRAS!C1:AA500),FLATTEN(MUESTRAS!D2:AB501),,0)
See how the ranges are moved one cell down to the right. Adapt them if needed. You can use this as an ArrayFormula too:
=INDEX(XLOOKUP(A2:A,FLATTEN(MUESTRAS!C1:AA500),FLATTEN(MUESTRAS!D2:AB501),,0))
I'd like to do something like a Google Sheets array, but have it display in a single cell.
Given a column of data (grades, for example):
={MAX(A1:A10), MIN(A1:A10)}
where the output inside of a single cell would look like (High Score, Low Score).
This currently works, but displays the array across two side by side cells. I wouldn't mind if they displayed on top of each other, if one cell isn't possible. Is either of these options possible?
all you need is:
=MAX(A1:A10)&", "&MIN(A1:A10)
In addition to the answer I accepted, the following works in the case where you want to arrange the array vertically:
={MAX(A1:A10) ; MIN(A1:A10)}
I need each line in this one cell to be transposed into individual cells along the same row.
I used to be able to use something like this:
=split(regexreplace(R2, "(.{10})", "/$1"),"/")
Now I can't seem to get it
=split(A1,Char(10))
(I am not prepared to type in all of the numbers OP could not be bothered to present as text just to show the above works, nor guess what underlies the #REF!.)
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.