I have a table view and I want to include margins so that the table's content has some breathing room on the left and right, and also between cells.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I managed to do a table view like:
My Storyboard design is like:
What I did was I added a UITableView
to the main view and gave a margin of 10. I added constraints as shown in figure. Changed the Seperator style
to None
.
Then I added two UITableViewCell
s.
- For Holding the data with custom row height 70.0
- Row with same background of parentView with custom row height of 10.0
And implemented the methods like below:
// Row count (Twice as needed, for showing the insect)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 15*2;
}
// Returns cell based on indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Decides whether content or inset
if (indexPath.row%2)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseInset"];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseMe"];
cell.textLabel.text = @"MMP";
}
return cell;
}
// Returns custom row height based on indexpath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row%2)
{
return 10.0;
}
return 70.0;
}