开发者

How to display a set number of rows in UITableView?

开发者 https://www.devze.com 2023-03-09 21:39 出处:网络
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?

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?


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.


Check the UITableView DataSource protocol, there are two relevant methods you have to implement:

– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection: 

The first one should return 1, the second one 4.


If you want to keep a set number of lines in your tableview and not display the extra blank lines, you could insert a empty footer view in viewdidload function.

self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];


I did not actually understand what you mean with "display on those 4 rows without any blank rows" but I guess what you ask is just a simple data retrieval process. I suppose the data source is NSarray.

-numberOfRowsInSection:(NSInteger)section{
  return [MyArray count];
}

-cellForRowAtIndexPath(NSIndexPath*)indexPath{
       NSInteger row=indexPath.row;
  cell.textLabel.Text=[MyArray objectAtIndex:row]
  return cell;
 }

I assume you know what should be put in those blank spaces.I just wrote the major points.


try this

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
   {
   return 4
   }  

also implements the rest of delegate methods required to populate tableview..

Hope it helps you.......


You can use this

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
   {
   return 4
   } 

Or you this also Check the UITableView DataSource protocol, there are two relevant methods you have to implement:

– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:

The first one should return 1, the second one 4.

Or you can also use GroupBy table in your view by changing the table style to gourpby table. use

0

精彩评论

暂无评论...
验证码 换一张
取 消