I am relatively new to Objective C/iOS development so I realize that this may seem like a basic question: For iOS 5+, I'm looking for a primer on how to create a tableview that has cells with a variety of data. For example, in my data array I might have data like:
Restaurant Name (basic string)
Restaurant Logo (string name of an image file)
Restaurant Rating (an integer)
In a single cell, I want to display the logo of the restaurant, then the name, and then show a number of stars based on the rating integer (ie a "4" would display a single star image 4 times).
I can't tell if I can do this with the standard table cell or if I need to create a custom table cell object.
Help!
Here is a good place to start.
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
One point they make is that if you subclass UITableViewCell then you have done something wrong. Instead, read it as most of the time customizing the standard UITableViewCell is sufficient.
For spectacular customization then deriving a class from UITableViewCell is fine.
I'll try to give you some suggestions on how to reach your goal based on my experience.
I think you can use both a default cell or a custom one.
In the first case, you have can use a simple cell (UITableViewCell) with a default style.
The properties you can use are: imageView for the logo, textLabel for the name and accessoryView for the rating.
Take a look to A Closer Look at Table-View Cells for info about table cell structure.
If you have to maintain a specific order for your cell content you need to create a custom cell that extends UITableViewCell and override layoutSubviews methods. I've already answered a question in this stackoverflow topic custom-uitableviewcell-with-an-optional-image.
In the second case you can use a custom xib or a completely fresh cell and do the stuff there. Out of there you can find lot of guides to do that. Here fews:
The one that #bbarnhart has written
custom-uitableviewcell
creating-a-custom-uitableviewcell-in-ios-4 (suggested)
P.S. For creating a rate view I suggest to take a look at how-to-make-a-custom-uiview-a-5-star-rating-view.
Hope it helps.
You can do this using a standard table view cell with an image and subtitle. The main text label is the restauraunt name, the image view holds your logo, and the subtitle can be made of the appropriate number of unicode star characters (for example, #"\u2605").
Related
still learning iOS development, want to create something like mention friend likes in Facebook / Instagram.
Mention People UI in Instagram
Is it using new TableViewController and add subview to the same View Controller? (in this case, CommentViewController) , but, when i already have UITAbleViewController in my CommentViewController, how can i handle the second tableviewcontroller?
Looking at the image you provided it looks as though the best way to implement this would be a UIViewController that has a UITableView added to it. Each tableview that is created can have a delegate and datasource set for it. When the textview detects that a mention is being entered (more about detecting this later) you would trigger a second tableview to appear as an additional view (subview) that overlays your current tableview (or as the accessory view of the keyboard, the way apple and others present a textview over the keyboard for text entry ex: messages app).
In order to manage the two tableviews my suggestion would be to create two additional classes each of which conform to the UITableViewDelegate and UITableViewData source. The first one would be the CommentsTableViewManager and the second would be the MentionsTableViewManager. The first tableview would set the CommentsTableViewManager as its delegate and datasource while the second would use the MentionsTableViewManager.
The other problem you may run into later on is determining how to properly detect mentions being typed into the textview. I've actually created an open source library that will help you with this problem. It's located here: https://github.com/szweier/SZMentionsSwift the README should provide enough information for you to get started if you choose to use it.
I hope the information about helps get you started with your app.
From architecture prospective it's way better to have a single table view with altered data source container, depending on current mode.
Speaking an instagram way - either you're showing comments, or, if # symbol was detected, displaying a list of users. So almost all your UITableView's delegate and data source methods will start with something like if (isMentionMode) and you'll choose specific cell class/cell's height/amount of rows per section/etc depends on isMentionMode state.
I want to add in an iOS App a list with 3 columns, but I also want that each field could be editable on the fly by clicking directly on it and when I press return it saves the field.
Currently I am using Xcode 4.6.1 and storyboard.
Here is the idea:
Because I am a beginner, I am asking you if there is a way to do that by using the UITableViewCell, UITextField and UITextView or there is something else to create this list faster.
Can you provide me some links with tutorials or similar, please?
Another question is: because each field (Label1,1; Label1,2; L1,3) will refer to the same object (eg.: cable; [size]1 meter; [diameter]1 cm), is it better to put each row in a NSMutuableArray or put everything in a SQLLite Database?
Hope in some hints.
You can create custom cell. I answered how to do it here
And add gesture recognizers for each object on the cell.
Change values and reload data.
Is there a way to display a different small image (icon) for each row of a NSTableColumn ?
I don't necessarily need to add a new column for it, I was wondering if I can just add the icon in front of the text of each row.
I know there is the method: - (void)setDataCell:(NSCell *)aCell . However this method seems to use the same cell for all rows, which is not what I want.
Is there a solution to this problem which doesn't require to subclass the NSTableColumn ? If not, what should I subclass ?
thanks
Make the cell for the column in question an NSImageCell. You can do this in Interface Builder by dragging an "Image Cell" from the object library onto the column. Then select the column and you'll be able to bind to an image source via one of the Data, Value, Value Path, or Valueurl bindings. Which one you choose depends on whether the source property in your model returns NSData, NSImage, NSString path, or NSURL url, respectively. See the documentation on NSImageCell bindings for more complete information.
Cells are the same for each row, because they're a lightweight class used as a performance optimization for drawing. That doesn't mean that each row has to use the cell to draw the same image though, any more than the text columns' cells have to draw the same string for each row.
If you really need the image to be in the same column as text, you'll have to do something more like duDE explains. Off the top of my head, I believe bindings should be still possible in this case, but you'll have to write more code to make that work. Rather than do that, I would probably switch to using a view based NSTableView, which makes putting multiple views in a single column/row easy, and will let you continue to use bindings without any extra work.
EDIT: duDE's answer was deleted, but to summarize, it suggested creating a custom NSCell subclass that would draw both an image and text.
Does anyone have any info on this? I'm new to cocoa, all tutorials seem to be for iPhone which uses a different view controller. Anyone willing to provide a step by step for adding labels to a custom cell? (I'm pulling from Core Data)
EDIT: It's important to note I'm using Core Data here. Many tutorials use arrays.. I don't understand why you would use that??
I'll award an answer quickly!
Zach
I'm not sure if this is going to help you, but your problem seems unrelated to your use of CoreData.
If I've understood your problem correctly, here are some steps:
Populate your NSTableView
Using CoreData, what you can do is put an NSArrayController object in your XIB document, set its mode to Entity and choose the Entity you want to display in your TableView (all of this from the first tab of the inspector on your array controller object).
Then, bind your NSTableView Content to the arrangedObjects of the array controller. You might also bind the selectionIndexes and use some sort descriptors on the array controller to order your data, as CoreData will give you a set rather than an array.
Click on your table view as many times as necessary as to select the table column in which you want your custom cell to appear, and set its Value binding to arrangedObjects too.
Set your custom cell
Finally, click on the cell of this table column and in the "Identity" of the inspector, change the class name of the cell to the class name of your custom cell.
I'll let you read the appropriate documentation to implement your custom cell according to what you want to achieve.
With the different bindings I've described, the objectValue of your cell should be the object from your array controller at the same index of the row your cell will appear on.
Please note that I've not tested again all of these steps but answered from memory... there might be details I've missed but you should have the main steps here.
I may have taken a million wrong turns to get to this question, so I'm happy to revise if someone can spot where I went wrong.
I am trying to build a tableview that looks the same as the Contacts app. My first issue is that an entry will have both a bolded and unbolded string in a given row like "John Appleseed" or "Martin Luther King". I figured I need have two UILabels within my UITableViewCell (possible my first mistake).
All I want to do is simply add that second UILabel so instead of subclassing I just add another UILabel in UITableViewCell (possible my next mistake).
However when a row is highlighted (but not yet selected) the default UILabel text switches to white, but my ad-hoc UILabel remains black. I can only force the UILabel text to change to white in the UITableViewDelegate methods which occur AFTER the highlighting. This cause I noticeable flash of white -> black.
Now I see the UITableViewCell method 'setHighlighted:animated:' which I could override if I subclassed, but I was hoping the were another way to approach it.
Thoughts?
I think you're going to find it difficult to keep the last name proportionally spaced from the other portion of the name in a separate view, especially given that the views may be resized by the table view in a number of situations (for example, to accommodate controls when the table view is in editing mode).
It should be a lot easier to create a custom subclass of UITableViewCell and override its -drawRect: method to draw the text itself. Take a look at the UIStringDrawing category on NSString for a list of messages you can send to an instance of NSString to tell it to draw itself either at a given point, or within a given rectangle.