UITableView - How to show rows only which contains data
How can i show UITableView
Rows only which contains data not the other rows. By default UITableView
shows 10 rows. If we have da开发者_如何学Cta for three rows It will display three rows only
How can I implement this? Thank you.
You can set a footer view and make it invisible if you want. Separators will appear only for rows which contain data:
self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];
Try this ..
self.tableView.tableFooterView = [UIView new];
I don't think it is possible to hide the extra separators using a standard way. This behavior is preset.
Plain
styled tableView shows the row/separator
even if there are no real rows exist. Only Grouped
styled tableView shows only the existing rows.
Edit: As suggested by others, you can add a footer view to tableView to hide those extra separators.
OK, I assume you are using Interface Builder.
The easy way:
make sure you have chosen your view controller class as both UITableViewDelegate and UITabbleViewDataSource.
In the ViewController.h (or whatever its called) add
<UITableViewDelegate,UITableViewDataSource>
before the { of the @interface definition. to the class that it must conform to those protocols.Make sure the minimal datasource protocol methods are in the class :
– tableView:cellForRowAtIndexPath: – tableView:numberOfRowsInSection:
in the – tableView:numberOfRowsInSection: method return the number of desired rows - 3 or by a bit of code to calculate this property
in the – tableView:cellForRowAtIndexPath: method do what you need to on the cell and return it. Boilerplate code
is:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease]; } cell.text = @"I am a cell"; return cell; }
N.B. If you are not using Interface Builder do: tableView.delegate = self; tableView.datasource = self;
Add a static cell that fills the rest of the screen.
Just add below code in your viewDidLoad() method.
self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
You can specify the required number of rows in the return parameter of the following function :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return NUMBER_OF_ROWS;
}
For more details please go through the Apple documentation of UITableView.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html
精彩评论