Chief Madog
Chief Madog

Reputation: 1507

How to change line length in UITableView swift

I'm using a tableview in my app that has lines not reaching until the end of the row:

enter image description here

In my design I need some of the lines to reach all the way like in this picture:

enter image description here

Also another problem I have is that I see the last line in the tableview:

enter image description here

My question is how do I make some of the lines all the way across like in the 2nd picture and another question is how do I remove the last line of the UITableView ?

Upvotes: 0

Views: 1164

Answers (6)

yasin89
yasin89

Reputation: 173

For Xcode 15 and Swift 5 on Storyboard

Inside Storyboard of Attribute Inspector make Sperator Inset Custom and you can give leading and trailing inset.

enter image description here

enter image description here

Upvotes: 0

mazenqp
mazenqp

Reputation: 355

to remove that last line or all other lines you have set tableView.tableFooter = UIView() in cellForRowAt

Upvotes: 0

Rakesh Patel
Rakesh Patel

Reputation: 1701

You can change insets of seperator for last cell in tableview so it will disappear from screen.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableviewCell", for: indexPath) as! TableviewCell

    if indexPath.row == LastCount {
        cell.separatorInset = UIEdgeInsets(top: 0, left: UIScreen.main.bounds.width*2, bottom: 1, right: -(UIScreen.main.bounds.width*3))
    }
    else
    {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    return cell
}

Upvotes: 0

Mehul Thakkar
Mehul Thakkar

Reputation: 12594

You can use below code for the tableviewcells in which you want it to be touched to edge of tableview.

cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero

Note: As tableview uses reusable cells, so if you are setting separatorInset and layoutMargins, then make sure you are setting it for all cells. Otherwise reused cell will have different separator inset then expected. So, for other cells, keep left=16 in UIEdgeInsets.

for case of hiding the separator case, you have to again play with separatorInset

 cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)

Upvotes: 2

gaurav bajaj
gaurav bajaj

Reputation: 106

  1. You are using custom cell so you can use label/view for line, set the label/view leading and trailing constraints or update label/view height, color according to your requirements and set the table view separator None.

  2. Set the condition for last row of table in Table cellForRowAt function and hide the label/view.

Upvotes: 0

Muhammed Azharudheen
Muhammed Azharudheen

Reputation: 181

add a view with light gray background with height of 1 and append to the top of cell. customize in each cell as you needed.

Upvotes: 0

Related Questions