Custom UITableView (UIView + Cells)
I already created a UIViewController, where I display text, picture and other information. Now I wanna be able to put somes cells (so a tableView) under this View. What is the best way to do th开发者_如何学运维at?
Do I have to create a UITableView and create an header with my picture, text.. or other possibility
Best Regards,
Ok I find a solution.
I created a simple UITableView. Inside that tableView I created a function:
-(void)setHeaderView {
self.myHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 300)];
self.tableView.tableHeaderView = self.myHeaderView;
}
And in ViewWillAppear I just have to call this function. So when I run the application it will display the View and under the View my TableView. I didn't know that was easy like that.
Best Regards,
In response to your post about the solution:
Not sure, but shouldn't you release myHeaderView
at the end of setHeaderView()
?
This depends on what you want the UITableViewCells to look. If you just want one line of text there is no need to create a custom cell, but instead create one within the code.
-(UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] dequeueReusableCellWithIdentifier:@"someId"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero
reuseIdentifier:someIdentifier] autorelease];
...
}
However, if you need a photo along with some text it will be easier to create a UITableViewCell.xib file, add the UILabel, UIImageViews etc, then when creating the cell within the method shown above replace the
cell = [[[UITableViewCell alloc] initWithFrame ...
with
NSArray *nib = [[NSBundle] mainBundle] loadNibNamed:@"YourNibCustomCellName" owner:self options:nil];
cell = [nib objectAtIndex:0];
精彩评论