无法在tableview中创建节边框,我想根据附加图像表示ui?

时间:2016-07-21 10:11:18

标签: ios objective-c uitableview sections

enter image description here

如何在tableview中创建节边框,我想根据附加的图像表示ui?我在部分之间显示了空格,但无法为部分设置边框线。

1 个答案:

答案 0 :(得分:1)

为此你需要添加和导入

#import <QuartzCore/QuartzCore.h>

之后按照以下步骤

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  UIView  *section = [[UIView alloc]init];
  section.frame = CGRectMake(0, 0, tableview.frame.size.width, height);
  UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"imageName"]];
  imageView.frame = CGRectMake(0, 0, YOUR_WIDTH, YOUR_HEIGHT); //set your frame
  [section addSubview:imageView];     

  labelSection = [[UILabel alloc]init];
  labelSection.textAlignment = NSTextAlignmentLeft;
  labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20); 
  [labelSection setBackgroundColor:[UIColor clearColor]];
  [labelSection setFont:[UIFont boldSystemFontOfSize:15]];
  NSString *name = @"section title";
  labelSection.text = name;
  [labelSection setTextColor:[UIColor blackColor]];
  [section addSubview:labelSection];


  section.layer.borderWidth = 2.0f;
  section.layer.cornerRadius =  1.0f;
  section.layer.masksToBounds = YES;
  section.layer.borderColor=[UIColor lightGrayColor].CGColor;  

  return section;
}

如果要设置行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return 44;  //It default size.If you want to change to other size you can change.
    //OR
 // return 90; //Set to your needed size
}