iPhone:How to override the cell's layout after I load it from NIB
I design a cell in IB, and load that cell in UITableview using "loadNibNamed" and it is handy.
But sometimes I开发者_运维问答 want to be able to overrride some of its layout when needed, for instance if there are UILabel and UITextfield, I want to be able to change the width of the Label for instance..if I override this cell layout do I have to define everything in that cell again, or I can overrride whatever control I want.
And also how and in which method can I do this?
Set tags in intefacebuilder to the uikit objects, you want to modifiy, eg an uilabel.
In
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
where you setup the cell call
[(UILabel*)[cell.contentView viewWithTag:YOURLABELSTAGHERE] setFrame:LABELSNEWFRAMEHERE];
You need to declare IBOutlets for those elements you want to alter. For example if you loaded a NIB that has a UIButton that you want to change the text of somewhere in your code, you need to create an outlet to that element like so:
.h
@property (nonatomic, retain) IBOutlet UIButton *myButton;
.m
@synthesize myButton;
... Last step is to create a link in Interface Builder between your outlet and the visual representation of the button. One way to do this is to right click on the File's Owner icon on the left bar. You should see 'myButton' listed under outlets. Simply drag a connection from this to the actual UIButton on your view to link it.
Now you can do stuff like this in the class you defined the @property in:
self.myButton.text = @"Button";
精彩评论