How do I access UITableViewController parameter from within a subclass'ed UITableViewCell?
I have a parameter in the UITableViewController for the font size (i.e. users can change font size). So the layoutSubviews method in my custom subclassed UITableViewCell will need to access the latest font when it needs to re-layout itself (as it label positions will depend on the font).
So my question, from with my custom subclassed UITableViewCell, and specifically within the layoutSubviews method, how do I access the "uiFont" parameter which is an instance variable from the UITableViewController?
There are a few ways to do it: access the UITableViewController via a property on the application delegate (which you can access from anywhere using [UIApplication sharedApplication].delegate), give the cell a reference to the UITableViewController when you create it in tableView:cellForRowAtIndexPath:, or follow the UIResponder chain from the cell view until you find a UITableViewController.
But really, this is probably a poor architecture. You should probably just call reloadData on the table view to have it recreate all the cells, and set the font in tableView:cellForRowAtIndexPath:. Or if the font setting should persist, you could store it in NSUserDefaults and have the cells listen for NSUserDefaultsDidChangeNotification.
Accessing the UITableViewController object from within your cell isn't a good approach in terms of design. What you should be doing is creating an ivar in the table cell itself to store the UIFont object:
#interface CustomCell : UITableViewCell {
UIFont *font;
}
#property (nonatomic, retain) UIFont *font;
And then in your tableView:cellForRowAtIndexPath method, set the font of the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setFont:uiFont];
...
}
Related
I have custom cell created with nib. In the table view I am using the method -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath to set the height of cell
Everything works fine.
But the problem is that I also want to change the size of UIlabel which is added as subview in nib of cell.
How do I do that?
Which metod to override in customcell class ?
The method you are looking for is:
-(void)layoutSubviews
{
[super layoutSubview];
//Do your magic
}
layoutSubviews is call after the cell is created and whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations, but in this case you can also use it to redraw your subviews.
As you have UILabel in your custom cell class, make a function in that class that take frame you want to set as parameter. Set frame of label in that function. You need to call this function from your cellForRowAtIndexPath method before returning the cell.
If you are reusing your custom cell you should call method only when the (cell == nil)
Also if you can add some code in your question that would be helpful and you can get more precise answer.
You need to treat your custom cell in the same way you would treat a normal view or view controller class with a xib. i.e. You need to create IBOutlets for the controls in you custom cell and during creation of the cell you can access the controls quite easily.
myCell.myCustomUILabel.text = #"blah"
There are some gotchas when using custom cells in a xib and onnecting up the IBOutlets. This SO answer (of mine) explains how to create and link up IBOutlets of a custom cell.
I am using the xcode template to create a UITableView application with UINavigationController.
I need to add a UIView (at fixed position) between the UINavigationBar and the UITableView. How to do that ?
Thanks.
You can do this by setting the UITableView's tableHeaderView property.
UITableView class reference
tableHeaderView
Returns an accessory view that is displayed above the table.
#property(nonatomic, retain) UIView *tableHeaderView
Discussion The default value is nil. The table header view is
different from a section header.
Availability Available in iOS 2.0 and later.
Add UIView and UITableView as subviews of UIView and then position their height, width and x, y position from the Size Inspector in Interface builder or Use CGRectMake in objective-c
you need to implement following UITableview delegate methods
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Step 1)
Create a header view as, set outlet to that view.
#property(nonatomic,strong)IBOutlet UIView *viewHeader;
Setp 2)
Add following single line code on viewDidLoad
[self.tableview setTableHeaderView:self.viewHeader];
Done!!!
Views are maintained in a hierarchy in iOS. They are maintained essentially as an array with the latest view first. To insert some view below one view or above another you just need to manipulate this.
All this is theory, I have not done what you want. So dont hold a grudge against me ;) this is a friendly suggestion.
[self.view insertSubview:yourNewView belowSubview:navigationController];
I want two custom (i.e. subclassed) UIViews in a subclassed UITableViewCell as shown in the below picture. The two UIViews are the same subclass.
Both the custom UIViews and the TableViewCell have associated xib's.
I would appreciate some advice on the best way to go about this. I load the TableViewCell this way.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:NULL];
// CustomCell is an IBOutlet connected to the above nib
cell = BLCustomCell;
}
// configure the cell
}
I want to set outlets in the Custom Views to easily display data from my data model. Do I need a view controller for the Custom Views? I'm having trouble getting the nib to load for the Custom Views. (And yes, I realize my code above does not address this issue.) How do I get it to load? Does the TableView's controller need outlets to the Custom View objects?
Thanks!
The simplest way to handle complex UITableViewCells is to create a subclass of UITableViewCell, with its own IBOutlets that connect to the subviews, then just set properties of your custom cell in cellForRowAtIndexPath:. There are various other approaches, but this one seems to break down the problem reasonably well, and expands to handle more complex situations.
Take a look at the iOS Recipes book by Matt Drance, it cover this area well.
I want to use -drawRect: in an UITableViewCell subclass but it is covered by contentView. So the best option seems to be that I make a UIView subclass with my -drawRect: code and use that as contentView. But how could I feed my UITableViewCell subclass with that contentView?
UITableViewCell creates that on its own when the contentView property is accessed. Would I simply override the getter method and then provide my own view there?
Make a subclass of UIView and implement your drawing code in this objects drawRect method.
Add this subclass to the contentView of the cell upon initialisation.
[cell.contentView addSubview:customView];
No need to modify the cell's drawRect method.
I have a tableView that's loosely based on the DetailViewController in ye olde SQLiteBooks. It's created programatically. Each cell displays a single value that when clicked displays a generic editingController that allows the user to edit that value. Pretty standard Cocoa-touch stuff...
Except...I also have a segmented control in the header of said tableView that depending on it's setting needs to change an attribute (textColor) on a UILabel in ONE of the 8± tableViewCells in the table. I have no IBOutlets for this tableView because I created it entirely in code. So, what do I need to do in my (void)segmentAction:(id)sender method (triggered when segmentedControl changes state) to allow me to access & change this value and display it to the user? When the table was built (cellForRowAtIndexPath) every UILabel was called "value" and then added: [cell.contentView addSubview:value].
I've tried setting a property of the viewController itself that is then checked during cellForRowAtIndexPath and does the textColor business there...and then adding [self.tableView setNeedsDisplay] to my segmentAction: method but it doesn't seem to work?!?
Any ideas?
If you know the indexpath of the cell you want to modify, you can call cellForRowAtIndexPath: to retrieve the UITableViewCell. Once you have that, you can get the UILabel from it. If you set the UILabel's tag to some useful value when create it, you can then retrieve it from the UITableViewCell via viewWithTag:.
In the class that implements cellForRowAtIndexPath you could also store a reference to the UILabel that you want when you build the relevant cell.
This would then mean you could alter the label and mark it to be repainted.
Alternately you could hold a local BOOL to show that the cell should be drawn differently - and then use this this in the cellForRowAtIndexPath: to draw it with a background.
You might find this easier with the 3.0 textLabel object in a UITableViewCell