简体   繁体   中英

How to remove the bottom line of UITableView in iOS

Whenever I create a tableView,there will be a line at the bottom of the last cell,is there a method to remove it?I don't use the Xib or Storyboard,so please show me the code.Thank you very much!

Sorry I didn't describe the situation clearly,here is the Screenshot,I have two sections in this tableview,and there is a line at the bottom of the last cell

这是截图

The solution is really easy if you just take a custom cell.

Firstly drag and drop a cell in your TableView and then select the TableView to make it's separator as None like-

在此输入图像描述

Then Assign the UITableViewCell's class top the custom one.

在此输入图像描述

In the TableViewCell in storyboard, take your required Objects like Lables, images or whatever you need. Now, make sure you take a UIView of 1 px at the bottom of the cell. Connect the outlets and add the required constrains.

在此输入图像描述

Your Custom Class may look like -

CustomTableViewCell.h file-

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell

@property(nonatomic, strong) IBOutlet UILabel *customTextLabel;
@property(nonatomic, strong) IBOutlet UIView *separatorView;

@end

Now in the View Controller, just show or hide the separator at the bottom row of your section.

So, your Datasource methods may look like-

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }
    else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"sampleCell"];

    if(indexPath.section == 0 && indexPath.row == 1){
        cell.separatorView.hidden = YES;
    }
    else if (indexPath.section == 1 && indexPath.row == 0){
        cell.separatorView.hidden = YES;
    }
    else{
        cell.separatorView.hidden = NO;
    }
    cell.customTextLabel.text = @"Test"; //put whatever you want
    return cell;
}

You should get a outlook like-

在此输入图像描述

Hope this helps.

You can design your own custom footer for UITableView But i don't think by default tableView show any footer. As Your question is unclear and you haven't provide any screenshot or anything else. Create your custom view with your desired colour contents and then just assign it to your tableView.

self.tableView.tableFooterView = customFooterView;

Or Maybe you want to hide/remove the tableView LineSeparator There are two most easy approaches to do this. Either assign a clear colour to separator or assign none to separator.

self.tableView.separatorStyle = UITableViewSeparatorStyleNone;

or

self.tableView.separatorColor = [UIColor clearColor];

For removing last line ie last separator of tableview, you have to use below code in UIViewDidload

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

OR if you want to remove particular cell separator use inside cellForRowAtIndexPath method

    tableView.separatorStyle = UITableViewCellSeparatorStyleDefault;

    if (indexPath.section == 1 && indexPath.row == 1)
    { 
       tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    } 

[Swift] 设置属性 tableView tableView.separatorStyle = .none 隐藏单元格下方的行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM