Am a newbie to iOS programming. This is what am trying to do:
The user enters some text in the screen and it keeps getting added to a UITableView.
As usual, it's getting added from the top.
But I want to add it from the bottom i.e. each new message that's added is added above the rest/existing ones, and not below.
Can someone offer some pointer on this please!
Thanks
Priya
NSMutableArray's -addObject method appends the object to the end of the array. If you want to put it at the beginning, just use this method instead:
[inputArray insertObject:userInput atIndex:0];
There are other ways to put objects in the array and move them around. Take a look at the documentation:
NSMutableArray Documentation
Are you using CoreData? If so, maybe you can set a creation time and sort it descending?
If you don't use CoreData but store the data in some sort of array, just add new objects to the beginning of the array and then reload the data.
[array insertObject:data atIndex:0];
Related
I see item UISearchBar search two arrays and very much items and not found solution, the problem its similar, have two NSMutablesArrays "subdetail" and "sublista" its show in Cell cell.textLabel and cell.detailTextLabel.
I try UISearchbar but i tray with NSPredicate and not run, try with NSRange and have more errors, i am desperate.PLEASE help me, any comments agree.
This its my code in Search:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSMutableDictionary *playas = [NSMutableDictionary dictionaryWithObjectsAndKeys:sublista, #"nombre", subdetail, #"localidad", nil];
[alldata addObject:playas];
for(NSDictionary *playas in alldata){
NSString *nombre = [playas objectForKey:#"nombre"];
NSRange nombreRange = [[nombre lowercaseString] rangeOfString:[searchText lowercaseString]];
if(nombreRange.location != NSNotFound)
[filteredList addObject:playas];
}
}
Add rest of code .m and .h
https://dl.dropboxusercontent.com/u/6217319/BuscarViewController.h
https://dl.dropboxusercontent.com/u/6217319/BuscarViewController.m
Thanks in advance.
BEST REGARDS
Your code has a lot of issues with it. I don't know which ones are actually breaking it, but any of these issues could be causing severe problems.
First, the code sample you provided doesn't provide declarations for a lot of hte objects you're referencing. No way this could possibly compile as given. If these are properties of the view controller instance, you need to use the accessor methods -- self.alldata or [self alldata], whichever you prefer.
Second, it looks like you're just adding to these properties. If you never reset their contents, every time this method is called, you're going to increase the size of your data set -- looking at your code, possibly recursively.
Third, you try to merge the two datasets together, and then try to search through only one of them. Either don't merge, or search through both seperately and then merge the results. As it is, what you're doing won't work.
Fourth, your table view should really only be displaying one type of data, so you shouldn't need to merge.
Edit:
Based on the code samples you've provided, your entire VC is going to need restructuring. Given how much of your code is written in what appears to be spanish, I'm doing a bit of guesswork at what you're actually doing.
First off, assuming you're using a modern version of xcode, get rid of the #synthesize in the .m file (it's no longer needed anymore). That will cause every single place you're using the actual ivar instead of a proper getter to turn into an error, so you can fix them quickly. iVars will by default use a prefixed underscore of their property name, so you can still access them if you have to -- but only by explicitly accessing the ivar instead of the property.
Second, you should restructure how you handle the data. I don't know where you get your data, but it looks like the various objects are fairly consistent. You should go ahead and either create a new class to hold all the data, or just put them all into a single dictionary. At that point, the 'alldata' property should, in fact, be an array of all valid data. What you should do is then have a filtered list of data (filteredData would be a good name), and you place whatever data matches the search criteria in there. Just remember to either reload the table or update it appropriately as items move into and out of the filtered list.
In a certain view I have a bunch of AVAudioPlayer properties, each one is supposed to be played upon a certain user action. While the selected AVAudioPlayer is played, if another AVAudioPlayer was played - it should stop.
To manage that, I've created an Array that holds all the AVAudioPlayer properties and upon user selection, before playing the selected AVAudioPlayer, I wish to go over all the AVAudioPlayers and stop them.
The problem is that the reasonable place to create the Array is at the beginning (let's say, in ViewDidLoad) and at this point none of the AVAudioPlayer properties went through alloc+init - so if I look at the Array in the debugger it shows as empty (number of objects = 0). Currently, I do the alloc+init action only when the user is actually selecting a certain AVAudioPlayer.
I can do alloc+init for all the AVAudioPlayers at the beginning as well, but that will take resources that are not necessarily required.
Is there a way to create this Array without taking the required resources? i.e. create the array with "empty" objects and later have them be allocated and initiated?
I have a similar situation. What I've been doing is:
NSArray *myArray = [NSArray arrayWithObjects:#"",#"",#"",#""];
in viewDidLoad (if my array is going to have 4 objects). Then as I have the information available, I do:
[myArray replaceObjectAtIndex:myLocationToAddValue withObject:myObject];
I got the idea for this looking at some code in a very old project written in a completely different language by someone else from my company. It seems to be working for what I need, and it does have the added benefit that I can loop through my array and check
if ([[myArray objectAtIndex:i] length] == 0)
to see where I have items already. I should note that the objects I'm adding are going to all be NSStrings - if this loops through and finds an object has been put into the array that isn't a string (or, more generally, doesn't have a "length" method), I'm guessing some nasty stuff would happen, but I haven't checked for that yet.
I'm certain there must be a much better solution, but I'll put it out there since there haven't been any answers to this yet. I figure a sloppy answer that seems to be working is better than no answer.
I have made an iphone application, in which I have put tableview , the data in tableview is added by selecting datetime picker ,whenever we select any date time from picker it will get add into the tableview. Now I want to put all these added value of datetime into an array.
how can I do this. I am searching this concept from last many days. But got no answer. Please if some one know, help me.
Thanks alot.
NSMutableArray is what you'll want. It provides a method called addObject:, so you can add these date values to your array with ease.
Edit: You asked for an example, very well. Here's how you can do it and how you should approach such problems in the future:
1) You said you had a UIDatePicker instance that you wanted to save the date of. So how do we get the date? The first thing you should always do is consult the Apple Developer Documentation, in this case for UIDatePicker. A short read reveals that this class has a date property.
Aah UIDatePicker, is there anything you can't do?
Now that we have the date we want, you'll want to put it into an NSMutableArray, as I have suggested. So we do it the same way again - open the Apple Developer Documentation and look up NSMutableArray. That reveals to us that there is in fact a addObject: method but wait...there's more?? Yep, NSMutableArray is damn powerful, so have a look through the entire document and see what you can use.
The documentation reveals that we can use addObject: to add an object to the array, so let's start:
NSMutableArray*myDates = [NSMutableArray array];
Now we've initalised our array. Now let's add an object:
[myDates addObject:[datePicker date]];
Done. We've now added an object. If you want to save this disc at a later stage, use NSMutableArray's writeToFile:atomically: method. For more details, read the documentation.
Now don't just go ahead and copy and paste the code. Read Apple's documents on these classes, they are really helpful and will enable you to resolve such problems in the future.
When you add date in table view, add the same date to an array.
take an array in .h and then add date to array
[arr addObject:date];
In one of my Navigation view controllers I build an array of dictionaries to display in a table. Based on which one I select I then remove the dictionary from the array using
NSDictionary *notice = [notices objectAtIndex: roomIndex];
I create the new view controller using
Feed *notice_view = [[Notice alloc] initWithObject: notice];
I push the navigation view controller and I've implemented initWithObject which takes a Dictionary.
I release the notice and notice_view and all this works fine but if I selected go back, select it go back about the third or forth time the whole app crashes. If I dont release both of them it works fine no problems what so ever, except of course the memory leaks.If i only release one of them, either of them, it fails again. What gives? Should I not be using initWithObject or should I be passing it in some other way? I've also tried using autorelease but with the same result.
notice - you should not release, since you don't own the object(you are just using a object which is returned from NSArray) else retain this object when you retrieve the object from NSArray and release it later stage.
notice_view - as per you explanation I don't see any issue with releasing, I am assuming you don't have any reference to this object from other part of the code.
I'm guessing you'll want to get rid of [selectedNotice release], since there doesn't seem to be a corresponding -retain call in there.
My app is made up of a TabBarController, each tab with a UITableView.
On launch I parse an XML file from my server into an Object class and then display the objects in the first tableview.
My question is, what do I do when I want to parse a second XML file? Currently, when doing so, the information in "XML-file-2" will overwrite the objects parsed by "XML-file-1". How do I go about this properly? Do I set up another Object class for each XML file or is there another to work around the issue?
I am using NSXMLParser.
I think you should consider having two instances of XMLParser, one for each XML file you want to read. It allows you to read as many XML files concurrently without affecting each other. It is also more modular.
on line 21 of that snippet ( http://pastie.org/537227 ) you are setting the products array (appDelegate.products)to a new mutable array. if you want the second run to append to appDelegate.products, you should see if appDelegate.products already has objects in it, if so, don't assign a new array to it, just add to them to it using NSMutableArray's addObject: method
... Don't overwrite the data that's already there...?
If you're displaying the contents in a UITableView, then I'd be willing to bet you've got an NSArray in there somewhere. Hopefully, if you've set this up properly, the NSArray contains model objects, each one of which corresponds to one row in your UITableView. However, I would suggest using NSMutableArray. Then when you parse the second XML file and build your model objects out of that, just use NSMutableArray's addObject: method and then reloadData on the UITableView.
As notnoop already mentioned, to make multiple NSXMLParser instances would be the best solution.
A open source iPhone RSS reader called Simple RSS Reader would be a good sample of what you want now.
You might use RSSParser class of the Simple RSS Reader as it is.
HTH