Changing UITableCellView appearance before it is highlighted (from UITableView) - objective-c

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.

Related

NSTableView won't layout / resize columns until NSWindow manually resized

I've an NSTableView with several NSTableColumn objects that appear to have all the correct auto-resizing flags set. However, every time I rebuild the table's contents, the columns all return to a narrow size -- unless I click and manually resize the window.
The NSTableView is inside:
NSWindow
NSView
NSScrollView
NSTableView
(other NSTableView objects: NSTableColumn, NSTextFieldCell, NSScroller (x 2)
Column resizing mask is always:
NSTableColumnAutoresizingMask
NSTableColumnUserResizingMask
The table is created always set with:
[theNSTableView setColumnAutoresizingStyle:NSTableViewUniformColumnAutoresizingStyle];
After reloading the table with data,
[theNSTableView reloadData];
[theNSTableView tile];
...and even:
[theNSTableView setNeedsDisplay:YES];
All views are set to "autoresizesSubviews".
Neither the NSView nor the NSScrollView have any referencing outlets -- could that be the problem?
After discussing this issue at length with Apple Developer Technical Support, they believe there may be an issue with Carbon-Cocoa integration.
However, they also point out that I really should not set NSTableViewUniformColumnAutoresizingStyle and I should be calculating the widths of all my columns in my own code, and then either telling the column to remember its width, or storing the width and making sure to set the same width on each column AFTER I programmatically create the column.
While I can accept this, I had been at least hoping I could programmatically invoke the same method that is called when a user double-clicks on a column divider and the column resizes itself to fit all of its cell text contents. However, DTS tells me those functions are not available.
I have come across other solutions to this issue here on SO and I will see if they can be adapted to create an optimal solution.

iOS 6.0 UICollectionView - same cells being added multiple times

While using collection view, in cellforItemAtIndexPath, new cell instance is being added multiple times (on top of another) at same location/frame, even though correct reuseidentifier is being passed to "dequeueReusableCellWithReuseIdentifier:forIndexPath:"
The cell mentioned above is a subclass of UICollectionViewCell and contains UITextField with proper frame. When scrolling and textfield is first responder, the above said problem is occurring.
Please let me know of any pointers to address the issue.
This could be a bug in UICollectionView related to one I've already filed with decoration views. As long as your cells are opaque, it should not affect your interface
It's possible (though, imo, not very likely) that this is correct behaviour for UICollectionView and it uses those extra cells for interface orientation. At any rate, the problem seems much less pronounced that the one with decoration views, which would add dozens of copies of the decoration view. As long as it's not affecting your app, I'd say live with it.

UITableViewCell Height Calculation and delegation?

I know this question had been asked hundreds of times before, but it's never really been solved (Or at least the way I'd like it to be). I have a rather complex UITableViewCell setup. The cell.backgroundView is loaded from a UIView subclass which uses a fair bit of CoreGraphics and CoreText. The code is rife with CTFramesetterSuggestFrameSizeWithConstraints, so I'm relectant to duplicate the class in the heightForRowAtIndexPath method.
I think I can solve this by creating an NSMutableDictionary with the indexPath as the key and the height as the value. But then I'm faced with the problem of heightForRowAtIndexPath being called first. I believe I can solve this problem by guessing the height of the cell and then once the cell's UIView subclass has finished rendering, use delegation to set the cell's height.
But this leaves me with the problem, how the hell do I delegate this? And, how to I prevent scrolling from being extremely choppy? as the cells will be created and resized in a split second.
In the past, I've used a dummy cell. I have a separate method -fillInCell:forRowAtIndexPath: which puts data into the cell. That way I can fill out the dummy cell in -tableView:heightForRowAtIndexPath: and the real cell in -tableView:cellForRowAtIndexPath:.
If this does not work for you then there are other options.
The first thing that comes to mind is create real cells in -tableView:heightForRowAtIndexPath: instead of -tableView:cellForRowAtIndexPath:. You can store completed cells in a mutable dictionary. -tableView:cellForRowAtIndexPath: will simply pull the completed cell from the dictionary. You should also detect when scrolling has stopped so you can empty your dictionary (just because -tableView:heightForRowAtIndexPath: was called doesn't mean -tableView:cellForRowAtIndexPath: will get call for the same indexPath).
Hope that helps.

NSTableView: Avoid Blue Header when Sorting Table Column

when I click on the header of an NSTableView column, the header gets blue and the little grey arrow shows up. How do I avoid the blue selection and the arrow (but keeping the sorting itself)?
As an example for what I want: In Xcode click on the 'Groups & Files' header.
Thank your for any help!
If you are working with a multi-column table you should never try to disable the arrow and highlighting, how else is the user going to know what the sorting is... If you are working with a single column table (in a source list manner) then lose the header and go with the source list style for the table. Before deciding to override standard behavior you should read the Apple interface guidelines and consider if you really need override those behaviors. For the record Xcode's Groups & Files doesn't sort and that is why it doesn't turn blue or get an arrow.
You can control what shows in the indicator with NSTableView's instance method "setIndicatorImage:inTableColumn:". I don't know if there's a way to just change the highlight as easily.
You can get more control, including the highlight if you subclass NSTableViewCell and override the methods "drawSortIndicatorWithFrame:inView:ascending:priority:" and/or "highlight:withFrame:inView" to control both the indicator element and highlighting. Then you can use this in your table column like so:
NSTableHeaderCell *headerCell = [[[CustomHeaderCell alloc] init] autorelease];
[tableColumn setHeaderCell:headerCell];

What is the proper way to align UITableViewCells when only some have an imageView?

I am new to iPhone programming and working on my first real application (i.e. one not written in a book or online) and I've run into a small problem which I could solve a multitude of ways, but feel like there should be a good solution that perhaps I am just missing.
Here is the scenario: I have a UITableView with a bunch of standard UITableViewCells in it. What I want to do is toggle a green check mark when the cell is selected and I have that part working (note: I'm already using the accessoryType for something else, so I can't use it for the checkmark...besides, it's not as pretty). Unfortunately, when I toggle the checkmark like so:
if (...) {
cell.imageView.image = [UIImage imageNamed:#"checkmark.png"];
} else {
cell.imageView.image = nil;
}
It makes the cell's label bounce back and forth depending on whether it is checked or not. What is the proper way to align the cell's text (set via cell.textLabel.text) regardless of whether or not it has an image set? The solutions I have come up with are:
Create a blank 40x40 png image in Photoshop and set the unchecked to that
Create a blank 40x40 image solely in code
Set some setting that I don't know about that will align it for me
Create a subclass of UITableCellView that does what I need (which would be stupid, I'd just go with option 1...)
Suggestions? Thoughts? Comments? Thank you very much :-)
P.S. I'd like the solution to work with OS 3.0 and 4.0 if that makes any sort of difference.
Option 1 is the way to go for a standard cell.
Strictly speaking, you do not have to subclass UITableViewCell to customize the layout. You can add any views you wish to the contentView. So you can add a UILabel and UIImageView to the contentView instead of using the imageView and textLabel properties.