I have developed an application in which I am using this: BTSimpleSideMenu
In my app I am passing cell label's through this:
-(void)show{
sideMenu.delegate = self;
int count;
count = [rssOutputData count];
for (int i = 0; i < count; i++){
NSString *items = [[rssOutputData objectAtIndex:i]xmltitle];
BTSimpleMenuItem *item = [[BTSimpleMenuItem alloc]initWithTitle:[[rssOutputData objectAtIndex:i]xmltitle] image:[UIImage imageNamed:#"arrow.png"]
onCompletion:^(BOOL success, BTSimpleMenuItem *item) {
catLbl.text = [[rssOutputData objectAtIndex:i]xmltitle];
}];
sideMenu = [[BTSimpleSideMenu alloc]initWithItem:#[item] addToViewController:self];
}
[sideMenu toggleMenu];
}
In this code [[rssOutputData objectAtIndex:i]xmltitle] is actually the data which I am parsing from an XML file. I have a total of 5 entries in it that I want to show in the side menu, but unfortunately through this way the menu is only showing the last entry and only a single row.
I know this might be because it is overwriting entries on the first cell.
Please view the link given above and please help me out with this.
This code will solve your problem your are creating BtSimplemenuitem and BTSimplesidemmenu in every iteration so it will make a lot of sidemenu just change your code to this will work
-(void)show
{
sideMenu.delegate = self;
int count;
NSMutableArray *itemsArry = [[NSMutableArray alloc] init];
count = [rssOutputData count];
for (int i = 0; i < count; i++){
NSString *items = [[rssOutputData objectAtIndex:i]xmltitle];
BTSimpleMenuItem *item = [[BTSimpleMenuItem alloc]initWithTitle:[[rssOutputData objectAtIndex:i]xmltitle] image:[UIImage imageNamed:#"arrow.png"]
onCompletion:^(BOOL success, BTSimpleMenuItem *item) {
catLbl.text = [[rssOutputData objectAtIndex:i]xmltitle];
}];
[itemsArry addObject:item];
}
NSArray *itemSarry=[[NSArray alloc] initWithArray:itemsArry];
sideMenu = [[BTSimpleSideMenu alloc]initWithItem:itemSarry addToViewController:self];
[sideMenu toggleMenu];
}
Related
Ok, I'm a bit out of my league but here goes... I'm working on making some updates to an iPhone app for a client (it hasn't been updated since 2013...just to put into context how old the programming is). While making simple updates to the app, I noticed the twitter feed section kept crashing. I checked the debug and came up with this error.
[_NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
I know the array is empty... I'm just not sure why. Here's the code where I think it is filling the array
- (void)loadData {
NSURL *tutorialsUrl = [NSURL URLWithString:#"https://twitter.com/EdwinOrange/lists/kentucky-general-assembly"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
NSString *tutorialsXpathQueryString = #"//p[#class='TweetTextSize js-tweet-text tweet-text']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
NSString *strResponse = #"";
for (int l=0; l<[[element children] count]; l++)
{
strResponse =[strResponse stringByAppendingString:[[[element children] objectAtIndex:l] content]];
NSArray *_children = [[[element children] objectAtIndex:l] children];
NSLog(#"count = %d",[_children count]);
for (int k=0; k<[_children count]; k++)
{
NSLog(#"%#",[[_children objectAtIndex:k] children]);
NSArray *_internalChildren = [[_children objectAtIndex:k] children];
for (int j=0; j<[_internalChildren count]; j++)
{
NSLog(#"%#",[[_internalChildren objectAtIndex:j] content]);
strResponse = [strResponse stringByAppendingFormat:#"%#",[[_internalChildren objectAtIndex:j] content]];
}
}
}
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
tutorial.title = strResponse;
NSLog(#"strResponse = %#",strResponse);
NSLog(#"Data---%#",tutorial.title);
}
_data = newTutorials;
delegateObj.arrDetailContent = [_data mutableCopy];
}
It might also we worth it to note the Log does not return any information for the above code. I added an exception breakpoint which breaks at this line (because the _data array is empty)
Tutorial *data = [_data objectAtIndex:indexPath.row];
This may also help to track down the problem (this is at the top of the TwitterController.m)
#pragma mark - Calling Twitter feeds
-(void)getAndParseTwitterFeeds
{
[self loadAtData];
[self loadNames];
[self loadData];
[self loadImages];
[self loadHours];
[self loadLink];
[self loadImages];
[self loadHours];
}
-(void)getFeeds
{
[self getAndParseTwitterFeeds];
}
-(void)getFeedsLocally
{
_link = delegateObj.arrTwitterLink;
_objects = delegateObj.arrObjects;
_members = delegateObj.arrMemberName;
_data = delegateObj.arrDetailContent;
arrProfileImages = delegateObj.arrImages;
_hours = delegateObj.arrHour;
[self updateTableViewWithTheData];
}
Hope that is enough explanation... I'm really hoping someone can help me figure out why the array is not being filled. Thanks!!!!
EDIT: I'm starting to wonder now if the HTML parser is having trouble with images in tweets. This code is from around 2013 which pre-dates twitter's inline images. Could this be causing the array to return empty?
Also of note the arrays _link, _members, _hours, etc. are all being populated correctly and have identical coding up until the "NSString *strResponse" section for loadData.
I need to sort array (in IOS) like this...
let us suppose that i have an NSMutableArray with : 1,2,3,4,5
i need to build a new array with the last object in the middle and the middle will replace the last : 1.2.5.4.3...
please help:
this is what i doing for now, i got only the 1.2.5 but i not success to put the 4.3 ...
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
int arrCount = (int)arr.count/2;
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
for (int i = 0;i < arr.count; i++) {
if (i < arrCount) {
[arr2 addObject:arr[i]];
}
else{
index = i;
[arr2 addObject:arr.lastObject];
}
}
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#1,#2,#3,#4,#5, nil];
NSMutableArray *arr2 = [[NSMutableArray alloc] initWithArray:arr];
int middleIndex = (int)arr.count/2;
id middleObject = arr2[middleIndex];
[arr2 replaceObjectAtIndex:middleIndex withObject:arr2.lastObject];
[arr2 replaceObjectAtIndex:arr2.count-1 withObject:middleObject];
NSLog(#"%#", arr2);
-(void)Aray
{
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
if(Counter < NewColor)
{
[ColorArray addObject:[NSNumber numberWithInteger:ColorTemp]];
Counter += 1;
}
}
-(IBAction)Go:(id)sender
{
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
Color = [[ColorArray objectAtIndex:Index] intValue];
if(Color == 2)
{
ColorLabel.text = #"The Color is Black";
Screen.image = [UIImage imageNamed:#"BlackTile.png"];
}
else
{
Screen.image = [UIImage imageNamed:#"Tunnel.png"];
ColorLabel.text = #"The Color is Green";
}
Index += 1;
}
-(IBAction)Black:(id)sender
{
ColorTemp = 2;
NewColor += 1;
[self Array];
}
-(IBAction)Green:(id)sender
{
ColorTemp = 1;
NewColor += 1;
[self Array];
}
The issue is that the ColorArray needs to be an instance variable (or #property) of the class so that it persists outside of the method calls.
This code will always crash, regardless of the value of Index:
NSMutableArray *ColorArray = [[NSMutableArray alloc] init];
Color = [[ColorArray objectAtIndex:Index] intValue];
Color appears to already be an instance variable (or #property), so this concept should not be alien to you.
Side note: variables conventionally start with lower case and use camal-case naming.
I would like to create an array for iOS platform like the PHP syntax below, many thanks ~~
$createArray = array ();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
Save your values in NSDictionary and add that dictionary into your array
NSMutableArray *theArray = [NSMutableArray array];
for (int indexValue = 0; indexValue<10; indexValue++) {
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[theArray addObject:theDictionary]
}
While Retrieving time,
NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:#"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:#"age"];
you might find it helpful:
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[subArray addObject:[NSNumber numberWithInt:0]];
}
[array addObject:subArray];
[subArray release];
}
also check this question
Try this.
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 2; j++) {
//Do your Stuff
// [subArray addObject:name];
// [subArray addObject:Age];
}
[array addObject:subArray];
}
OR
Why can't try with the NSDictionary
You can use this. But this is not better way in IOS.
NSMutableArray *array[20];
for (int i=0;i< 20; i++)
{
array[i] = [NSMutableArray array];
for (int j=0;j<3;j++)
{
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[[array[i] addObject:theDictionary]
}
}
First you to have set An NSMutableDictionary on .h file
#interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}
I´m using this method to initialize an nsmutablearray
- (void)getAllContacts
{
Contact *contact = [[Contact alloc] init];
self.allContacts = [[NSMutableArray alloc] init];
int i=0;
for (i=0; i<5; i++)
{
contact.nome = [[NSString alloc] initWithFormat:#"Bruno %d", i];
[self.allContacts insertObject:contact atIndex:i];
}
}
Pretty straightforward! But right after, i do a for to print it´s elements like:
for (int i=0; i<[self.allContacts count]; i++)
{
Contact *c = [self.allContacts objectAtIndex:i];
NSLog(#"i=%d\nNome:%#", i, c.nome);
}
And it will show me 5 times the last element "Bruno 4". It doesn´t start from 0 and increments. What should i do to start from 0?
Try this:
- (void)getAllContacts
{
Contact *contact = nil;
self.allContacts = [NSMutableArray array];
int i=0;
for (i=0; i<5; i++)
{
contact = [Contact new];
contact.nome = [NSString stringWithFormat:#"Bruno %d", i];
[self.allContacts addObject:contact];
[contact release]
}
}
and please take a look at: Memoy Management
Because you are inserting the same object 5 times into the array. You need to create a new Contact object at every execution of the for loop.
What you are doing is you're actually adding one instance of the Contact class 5 times in the array and only changing nome property. Here is the correct way to do this:
- (void)getAllContacts
{
//alloc init returns a retained object and self.allContacts calls the setter, which additionally retains it.
self.allContacts = [[[NSMutableArray alloc] init] autorelease];
int i=0;
for (i=0; i<5; i++)
{
//Create the Contact object
Contact *contact = [[Contact alloc] init];
//Set the nome property
contact.nome = [NSString stringWithFormat:#"Bruno %d", i];
//Add the instance to the array
[self.allContacts addObject:contact];
//Release the instance because the array retains it and you're not responsible for its memory management anymore.
[contact release];
}
}