I'm trying to put a button to footer of TableView.
This is what i did and I can't see the button but the string "RESULT" is displayed.
I've tried everything I can think of. but I couldn't figure out what's wrong. I need you help!
- (void)viewDidLoad {
btnSeeResult = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSeeResult setTitle:@"RESULT" forState:UIControlStateNormal];
viewResu开发者_Python百科lt = [[UIView alloc] initWithFrame:btnSeeResult.frame];
[viewResult addSubview:btnSeeResult];
self.tableView.tableFooterView = btnSeeResult;
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return viewResult.bounds.size.height;
}
Anyway, what's the difference between setting the footer view directly like
tableView.tableFooterView = footerView;
and using (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
method??
There's no reason for the viewResult
wrapper. A UIButton is a UIView, so there's no reason to wrap it in another UIView in this case - especially since you then assign the button to the table's footer property which means the table should be taking control of how and when the button view ultimately displays.
You may have to set the btnSeeResult
's frame so that it has a height set. It's possible the height is 0 by default in that case since you don't ever set a frame for it. You might also try something like [btnSeeResult sizeToFit];
after you set the title which should resize it enough to make room for the label text, at least.
As to your second question, the difference between tableFooterView
and -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
is that the former defines a view to attach to the bottom of the entire table, whereas the delegate method allows you to give each sub-section in your table different footers. Both can be used at the same time and in that case the last section's footer view would appear followed finally by the tableFooterView
.
SeniorLee,
Welcome to StackOverflow!
You've got the right answer in your question - it's the latter code:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
When the table view goes to construct your table, it calls back to 2 classes - the table's delegate, and the table's data source.
Data Source is just what the name implies - the source of the data that is in your table. The Delegate, on the other hand, handles real-time interaction with the table and how it is to be displayed (e.g. what happens when the row is pressed?).
In many many cases, programmers set both the Data Source and the Delegate of the table view to the same class - the UITableViewController that owns the table view.
Anyhow, on to your question:
It's highly likely that the direct assignment isn't working explicitly because there is a delegate callback. When the table view goes to lay itself out, it will ask its delegate "do you have a view for the footer in section x?". That's the method above.
If you don't implement that method, the table view will have a default implementation that does something - in this case, probably set it to nil.
So, my guess is that even though you set this property directly in viewDidLoad, it's being overwritten to nil by the table view because you're not implementing the delegate callback. Instead, move your view code to the callback method and it should work beautifully.
Also, I don't think you need to wrap your UIButton in a UIView - UIButton is a subclass of UIView and thus should work just fine.
精彩评论