Here is the NSLog of my Structure (Each enum is a custom tableViewCell). I need to be able to show each cell in my table view.-
(
(
0,
0
),
(
4
),
(
1,
(
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
),
(
2
)
)
),
(
4
)
)
As you can see, it is an Array with 4 Array objects. Within these objects are enums, or arrays with enums. [0] has an array with two 0 enums, [1] has an array with one 4 enum, [2] has an array with one 1 enum, AND an array of arrays within that has eight enums of 2.
My question is how do I display these cells in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BucketListCellType type = [[[[self.presentedCells objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
}
is getting me an error - [__NSCFNumber objectAtIndex:]: unrecognized selector sent to instance
Here is where I am constructing that array -
-(void)showPresentedCells
{
NSMutableArray *preCells = [NSMutableArray new];
// First Section - Suggested Bucket List Images
NSMutableArray *suggestedBucketListImages = [NSMutableArray new];
[suggestedBucketListImages addObject:#(mainPhotoCellWithAddToBucketList)];
[suggestedBucketListImages addObject:#(mainPhotoCellWithAddToBucketList)];
[preCells addObject:[NSArray arrayWithArray:suggestedBucketListImages]];
// Second Section - Top Bucket List Images
NSMutableArray *topBucketListImages = [NSMutableArray new];
[topBucketListImages addObject:#(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:topBucketListImages]];
// Third Section - My Bucket List Images
NSMutableArray *myBucketListCategories = [NSMutableArray new];
NSMutableArray *categoryWithPictures = [NSMutableArray new];
[myBucketListCategories addObject:#(CreateNewBucketListCell)];
for (int i = 0; i <= [self.bucketListCategories count]; i++) {
[categoryWithPictures addObject:#[#(CategoryCell)]];
}
[myBucketListCategories addObject:[NSArray arrayWithArray:categoryWithPictures]];
[preCells addObject:[NSArray arrayWithArray:myBucketListCategories]];
NSMutableArray *unorganizedBucketListImages = [NSMutableArray new];
[unorganizedBucketListImages addObject:#(mainPhotoCell)];
[preCells addObject:[NSArray arrayWithArray:unorganizedBucketListImages]];
self.presentedCells = preCells;
}
Thanks for any help.
The source of the problem is that you're doing calling objectAtIndex for three levels of arrays, but that will work only where you really have three levels of structure (e.g. section 2, row 1). Everywhere else you only have two levels of structure (returning a NSNumber) and trying to call objectAtIndex with that will yield the error you describe.
What's less clear to me is what you want to show when there is only two levels of structure. But you could presumably do something like:
BucketListCellType type;
id object = self.presentedCells[indexPath.section][indexPath.row];
if ([object isKindOfClass:[NSArray class]]) {
type = [object[0] intValue];
} else {
type = [object intValue];
Related
Given: 2 dimensional array :
[[1 2 3]
[4 5 6]
[9 8 9 ]]
Need to add diagonal of the array : 1+5+9 = 15
NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObjects:#1,#2,#3,nil],
[NSMutableArray arrayWithObjects:#4,#5,#6,nil],
[NSMutableArray arrayWithObjects:#9,#8,#9,nil],nil];
NSNumber* total = 0;
for (NSNumber* row in array) {
total = total + array[row][row];
}
NSLog(#"%#",total);
Here I can't access the element of the array. It gives error on "array[row][row]". What is the best way to initialize a 2D array and access the element of the array in Objective-C?
In your code, what you are doing here: for (NSNumber* row in array) is putting each element of array, inside the row variable (this is called array enumeration btw).
What you need is to use the index of the array - not the value of each element (that's what you are doing above).
Also, you don't have to use an object (NSNumber *) for your calculations. A simple NSInteger will do.
Here's the code:
NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObjects:#1,#2,#3,nil],
[NSMutableArray arrayWithObjects:#4,#5,#6,nil],
[NSMutableArray arrayWithObjects:#9,#8,#9,nil],nil];
NSInteger total = 0;
for (NSUInteger row = 0; row < array.count; row ++) {
total = total + [array[row][row] integerValue]; // integerValue converts the NSNumber to an NSInteger
}
NSLog(#"%d",total);
I've NSArray with NSArray in. I would like to get all NSArray into one.
I think there is a simple function to use, isn't it ?
Here is what I receive :
(
(
n0eGi1KJWq,
KHGeW32548,
),
(
n0eGi1KJWq
)
)
I would like to get :
(
n0eGi1KJWq,
KHGeW32548,
n0eGi1KJWq
)
A simply loop can be used to create a new array:
NSArray *mainArray = ... // the array containing the other arrays
NSMutableArray *finalArray = [NSMutableArray array];
for (NSArray *innerArray in mainArray) {
[finalArray addObjectsFromArray:innerArray];
}
At this point finalArray will have all of the objects from all of the other arrays.
You can also use #unionOfArrays for this purpose:
NSArray *flatArray = [array valueForKeyPath: #"#unionOfArrays.self"];
If you need a deeper flattening (for three dimensional array), use it twice:
NSArray *flatArray = [array valueForKeyPath: #"#unionOfArrays.self.#unionOfArrays.self"];
I have this Mutable array that has this value
myArray = (
(
"1466759705.37136"
),
(
"1466761980.81194"
),
(
"1466990587.45011"
)
I want to remove the () on each element inside the array and make it like this
innerArrayOfDate = (
"1466759705.37136",
"1466761980.81194",
"1466990587.45011"
)
I tried myArray[0] to see if I will not include the parentheses but it still includes.
myArray[0] = (
"1466759705.37136"
)
You have array value which is inside of another array.
The value which you want is at 0 index of array so, you need to just add this myArray[0][0] instead of this myArray[0]
myArray[0] = (
"1466759705.37136"
)
myArray[0][0] = "1466759705.37136"
try this,
NSArray *myArray = #[#[#"1466759705.37136"],#[#"1466761980.81194"],#[#"1466990587.45011"]];
NSMutableArray *newArray = [[NSMutableArray alloc]init];
for (NSArray *array in myArray) {
[newArray addObjectsFromArray:array];
}
I have one problem and I want to search every object in my NSMutableArray.
if this object (the object for searching) no "0" add to another NSMutableArray with name : StarArray
this is my first NSMutableArray
(
(
1,
1,
6
),
(
0
),
(
0,
3
),
(
5,
3
),
(
0
)
)
this NSMutableArray is 2d (matrix) and I want get every object except object that have 0 value and add to StarArray
StarArray : {1,1,6,3,5,3}
Try this
It will take less iterations to execute
NSMutableArray *resultArr = [NSMutableArray array];
for(NSArray *arr in yourArr)
{
NSMutableArray *ar = [NSMutableArray arrayWithArray:arr];
while( [ar containsObject:#"0"])
{
[ar removeObject:#"0"];
}
if([ar count])
[resultArr addObjectsFromArray:ar];
}
Try this code, it may work:
NSMutableArray *StarArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [firstArray count] ; i++) {
for (id object in [firstArray objectAtIndex:i]) {
if (![object isEqualToString:#"0"]) {
[StarArray addObject:object];
}
}
}
NSLog(#"%#", StarArray);
I want to append an object in an array.
For example,
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<5; i++)
{
[array addObject:#[#"Any"]];
}
It gives an output like this:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any
),
(
Any
)
)
Now I want to append object at index 3 of an array so that it could appear like below:
array: (
(
Any
),
(
Any
),
(
Any
),
(
Any, Where, How, When
),
(
Any
)
)
Use function insertObjectAtIndex to achieve it.
[array insertObject:anObject atIndex:2];
If I understand you...
[array replaceObjectAtIndex:2 withObject:#[[[array objectAtIndex:2] firstObject],#"Where",#"How",#"When"]];
Try this:
NSMutableArray *array = [NSMutableArray new];
for (int i=0; i<5; i++)
{
[array addObject:[NSMutableArray arrayWithObject:#"Any"]];
}
NSLog(#"%#", array);
NSMutableArray *subArray = [array objectAtIndex:3];
[subArray addObjectsFromArray:#[#"Where", #"How", #"When"]];
NSLog(#"%#", array);
Note, #[] creates an instance of NSArray class which cannot be modified later. You can modify only instances of NSMutableArray class.