閱讀332 返回首頁    go 技術社區[雲棲]


UITableView去掉最後分割線的一種方法

UITableView以style:UITableViewStylePlain方式創建時,隻要有cell,就會有一條黑線 哪怕至於一個cell也會有,如圖

alt

在網上找了集中方法,都不好使,比如https://blog.csdn.net/l_ch_g/article/details/9290727,中的兩種方法,都嚐試不好使

第一種方法

 
1、加方法
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}
 
2、在

- (void)viewDidLoad

{

    [super viewDidLoad];

    //設置tableView不能滾動

    [self.tableView setScrollEnabled:NO];

    //在此處調用一下就可以啦 :此處假設tableView的name叫:tableView

    [self setExtraCellLineHidden:self.tableView];

}

 

在iOS4.3和iOS5.0中通過:值得注意的是在iOS4.3中可以直接設置footer為nil,但是在5.0不行,因為UITableView會默認生成一個Footer。(詳見iOS Release Notes中的說明:Returning nil from the tableView:viewForHeaderInSection: method (or its footer equivalent) is no longer sufficient to hide a header. You must override tableView:heightForHeaderInSection: and return 0.0 to hide a header.)

plain類型的tableview當顯示的數據很少時,下麵的cell即使不顯示數據也會有分割線,可以通過下麵這個函數去掉多餘的分割線。

 

- (void)setExtraCellLineHidden: (UITableView *)tableView

{

    UIView *view =[ [UIView alloc]init];

    view.backgroundColor = [UIColor clearColor];

    [tableView setTableFooterView:view];

    [view release];

}

當tableview的dataSource為空時,也就是沒有數據可顯示時,該方法無效,隻能在numberOfRowsInsection函數,通過判斷dataSouce的數據個數,如果為零可以將tableview的separatorStyle設置為UITableViewCellSeparatorStyleNone去掉分割線,然後在大於零時將其設置為

UITableViewCellSeparatorStyleSingleLine

 
第二種方法


if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];
}

// Setup default cell setttings.
...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;
...

In viewDidLoad:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

最後,創建UITableView時,使用style:UITableViewStyleGrouped,方法解決問題,

​代碼如下

1 self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
2  
3 _tableView.separatorColor = [UIColor clearColor];
4 _tableView.backgroundView=[[UIView alloc] init];    //改變表的背景視圖
5 _tableView.backgroundColor = [UIColor whiteColor]; //添加顏色

使用UITableViewStyleGrouped類型創建的UITableView,背景顏色需要使用上麵兩個方法設置的才能生效,普通的backgroundcolor方法無效。
同時由於UITableViewStyleGrouped模式默認會有Section高度,所以,要繼承下heightForHeaderInSection方法,記住在UITableViewStyleGrouped,直接修改
sectionHeaderHeight的方式是不行的。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    // This will create a "invisible" footer
    return 0.01f;
}


最後更新:2017-04-03 12:56:23

  上一篇:go 九度題目1341:艾薇兒的演唱會
  下一篇:go windows7加入windows2008R2域配置