How can I create a UITableView with selectable options? - iphone

I'm looking to create a grouped style UITableView that allows a user to select different options (like shown below). I'm already able to setup the UITableView in a grouped style; my question is based on how I can add buttons, toggles and list options like shown below? Is it possible to make use of plist's to simplify the process somewhat?
Note: I'm not trying to create a 'settings' view, I want to allow users to create lots of different NSDictionaries and set values on each of them using the table setup described.

You should create your own UITableViewCell. This way you could add the desired UI items of your choice. You could use the Interface builder to create your custom cell. There are many good tutorials on the subject.
I do not see the need to use a property list for this, since it is rather simple to implement this.
This is a good example:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

You should look a this library : http://www.inappsettingskit.com/
Other exist but I don't remember the name. Or you can define your own data structure in plist, and associate with UITableViewCell type. But this will require a lot more coding.

For adding a UISwitch to a UITableViewCell, you may want to have a look at the answer to this question on SO.

your answere lies in the delegate method didselectrowatindexpath. if you want to add options to the individual cells, you'll need either a custom uitableviewcell or set things in generic uitableviewcell accordingly. also, a notable mention would be to look into tableView:accessoryButtonTappedForRowWithIndexPath:
next you should use the plist as follows to read it:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"league" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
This could be called in your [uitableview cellForRowAtIndexpath:] delegate method and parse it accordingly
cheers~
fy

Related

iPhone - Slow scroll on TableView

i've problems with the scroll of my Table View. I have a custom cell that i load with this code :
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCellQuartieri" owner:self options:NULL];
cell=customCell;
}
This is the code. customCell is an UITableViewCell object with a own xib. The controller of the xib is my view controller where the table is placed in. I load 2 label and one imageView from the internet. What is the problem? And how can i make my custom cell reusable?
Thanks
To make your custom cell reusable, set the identifier propertie in Interface Builder.
Who do you load the data from the internet (Async)?
Since you are using a custom UITableViewCell you have to set the identifier string property through Interface Builder, and then dequeue the cell in the usual way, using that same string as the key. This way your app will not create a new cell for each row of the table, but will reuse the already present cells, reducing build and presentation times.
If this won't fix the issue, you should look at the internet connection, to understand why data loading is so slow. If you own the server that serves the data, you would try to speed it up, otherwise you should look for a different or more efficient way for loading data remotely. Some code example would be great.
Edit
As stated in the comments, the slowness could be related to the loading time of remote images. You could try to build a local dictionary, of something similar, in which you'll save the images you already loaded associating them to their URL as the key, while you'll read remotely those you still don't own. This will work like a local cache to improve loading time for remote data.
The problem was that images have to be loaded asynchronously. Search SDWebImages on Google.

Iphone, user created labels?

Usually When i want a object to be accesed by many different event calls i just declare it in the h file.
However im making a app where i need to allow the user to create UILabels and drag and drop them where they want. So i have the code to make the labels and a alertview where they change the text.
-(IBAction)AddText{
UITextField *TextView;
TextView = [[UITextView alloc] initWithFrame:CGRectMake(40.0, 40.0, 40, 40)];
[self.view addSubview:TextView];
Which works however i need the user to be able to create many textviews which each have a unique name and a way i can reference them later such as TextView.text, doing it this way always creates a object named TextView and i can only reference it from his event, otherwise its undeclared.
Im really stuck at how i would go about doing this if anyone could help it would be great!,
Thanks
You could think about adding the newly created label to a NSMutableArray or, better, to a NSMutableDictionary. This latter solution gives you the possibility of creating a dictionary in which each label can be referred to using a string key. If you put such a dictionary in your .h file you'll be fine, I think.
How about creating an NSMutableArray of NSDictionary items. The dictionary items could have a reference to the label as well as things like name, and other relevant meta information.
You can add the UITextViews to a collection of some sort, depending on how you want to retrieve them later.

UIButton simple subclass (need to have an NSString property like a tag)

I have a view that has a bunch of programmatically-created UIButton objects (rounded rectangle type), but I need to store a little bit of information in them, identifying each button. I was doing fine with the tag, but the numbers got too large, and I would like to basically have another piece of info associated with each button, this time an NSString. By the way, the label is taken, I can't use that. What's the simplest/quickest way to go about this? Do I need to subclass UIButton and add my own property to it like stringTag? I'm not super proficient in obj-c yet, and read somewhere this is a pain. Is there a simpler way to store an additional string in my UIButtons? Thanks a lot in advance for any advice.
Subclassing uibutton is a real pain. They don't usually work as expected since UIButton is an abstract class. Therefore to make a fully working button you may have to override quite a few methods to get the button to do everything the the Apple subclasses do. Instead if you just need to add storage you can use associative references. you must #import <objc/runtime.h> to use the functions.
The best way to do this is to define a string constant NSString *const buttonTagName = #"com.youapp.buttonTag"; and store tags with this as the key objc_setAssociatedObject(button, buttonTagName, tagForCurrentButton, OBJC_ASSOCIATION_RETAIN); .
I think adding an NSString with some id info shouldn't mess up your UIButton subclass too much. At least it's easy to try it out, we are talking 10 lines of code here. I subclassed a UIButton and overrided initWithFrame method once and got it working correctly. But it's true, it is a pain and don't ask me why I did it :)

Different methods to display NSarray

I have an array which store highscore records, besides using tableview, is there another way i can display my array.
That's the best way, if you're using an NSArray as a datasource; if you don't need the users to interact with the data, you could use a UIScrollView, maybe, by adding a UILabel for every data row, though this seems clunky.
Your other option, and probably the best option, is to customize the look of the tableview, as shown in this tutorial.

Display array in UITableView when in UIViewController

I have a tableview in my viewcontroller. But i have an error which say that [tableview:numberofrowinsection:]:unrecongized selector sent to instance. Can someone please tell me how cna i solve this.
You need to set the UIViewController as the delegate, and also datasource
also you need to implement the necessary methods.
if you look in the apple docs they are described there
but i think the main ones are:
numberOfSectionsInTableView:
tableView:numberOfRows:inSection:
and tableView:cellForRowAtIndexPath:
in the first one you return the number of sections, usually 1, in the second you return the number of rows for the section( usually the count of the array)
and in the final you return the UITableViewCell object with the necessary data put in position.
read the apple docs, and if necessary copy the method names from one of the UITableViewController templates.
hope this helps
Create a new file using the UITableViewController template - it has skeleton code that should help (even if you don't use it to build on, you can use it as an example of implementing the delegate methods and reusing cells).