Objective-c – How to display a set number of rows in UITableView

iphoneobjective cuitableview

How do i display set number of rows in a UITableView?

i.e if my data source has only 4 objects then i want the table to display on those 4 rows without any blank rows. any ideas?

thanks!

EDIT: i wasn't too clear about the question so..

So this is my table: http://i.stack.imgur.com/glkWZ.png ..
i want to display only the 3 rows and not the blank rows below it. The number of rows change depending on my data source.
any ideas how to go about doing so?

Best Answer

You have to change your table appearance, I think. Once you correctly set the numberOfSectionsInTableView: and tableView:numberOfRowsInSection: methods, you should change your viewDidLoad method adding this line:

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

This way you will remove all lines from the table.

If you still want to make a line appear between lines, I suggest you to create a custom UITableViewCell, or to build a standard one adding a subview to mimic the line. Eg. something like this:

CGRect lineFrame = CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,1);
UIView *line = [[UIView alloc] initWithFrame:lineFrame];
line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
[cell.contentView addSubview:line];
[line release];

Let me know if this helps.