를 사용자 정의하고 UITableView
있습니다. 마지막 셀 에서 구분되는 줄을 숨기고 싶습니다 …이 작업을 수행 할 수 있습니까?
나는 할 수 tableView.separatorStyle = UITableViewCellStyle.None
있지만 tableView의 모든 셀에 영향을 줄 것임을 알고 있습니다 . 나는 그것이 나의 마지막 세포에만 영향을 미치기를 원합니다.
답변
에 viewDidLoad
다음 줄을 추가하십시오.
self.tableView.separatorColor = [UIColor clearColor];
그리고 cellForRowAtIndexPath
:
iOS 하위 버전
if(indexPath.row != self.newCarArray.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor redColor];
[cell addSubview:line];
}
iOS 7 상위 버전 (iOS 8 포함)
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
답변
다음 코드를 사용할 수 있습니다 :
빠른 :
if indexPath.row == {your row number} {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
또는 :
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
기본 여백 :
cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)
엔드-투-엔드 구분 기호 표시
cell.separatorInset = .zero
목표 -C :
if (indexPath.row == {your row number}) {
cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
}
답변
Hiren 의 답변에 대한 후속 조치 .
의 viewDidLoad에 다음과 같은 라인 :
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
또는 XIB 또는 스토리 보드를 사용하는 경우 ” separator “를 ” none “으로 변경하십시오.
그리고 CellForRowAtIndexPath 에서 이것을 추가하십시오 :
CGFloat separatorInset; // Separator x position
CGFloat separatorHeight;
CGFloat separatorWidth;
CGFloat separatorY;
UIImageView *separator;
UIColor *separatorBGColor;
separatorY = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale); // This assures you to have a 1px line height whatever the screen resolution
separatorWidth = cell.frame.size.width;
separatorInset = 15.0f;
separatorBGColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];
다음은 동적 셀이있는 테이블 뷰를 표시하는 결과의 예입니다 (하지만 내용이있는 단일 뷰 만 있음). 그 결과 하나에 만 분리자가 있고 모든 “더미”테이블 뷰가 자동으로 추가되어 화면을 채우지 않습니다.
도움이 되었기를 바랍니다.
편집 : 항상 주석을 읽지 않는 사람들에게는 실제로 몇 줄의 코드로 주석을 처리하는 더 좋은 방법이 있습니다.
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
답변
구분 기호를 직접 그리지 않으려면 다음을 사용하십시오.
// Hide the cell separator by moving it to the far right
cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
이 API는 iOS 7부터 사용할 수 있습니다.
답변
내 개발 환경은
- Xcode 7.0
- 7A220 스위프트 2.0
- iOS 9.0
위의 답변은 완전히 작동하지 않습니다.
시도한 후 마침내 작동하는 솔루션은 다음과 같습니다.
let indent_large_enought_to_hidden:CGFloat = 10000
cell.separatorInset = UIEdgeInsetsMake(0, indent_large_enought_to_hidden, 0, 0) // indent large engough for separator(including cell' content) to hidden separator
cell.indentationWidth = indent_large_enought_to_hidden * -1 // adjust the cell's content to show normally
cell.indentationLevel = 1 // must add this, otherwise default is 0, now actual indentation = indentationWidth * indentationLevel = 10000 * 1 = -10000
답변
separatorInset.right = .greatestFiniteMagnitude
당신의 세포에 설정 하십시오.
답변
에서 스위프트 3, 스위프트 4 스위프트 (5)는 ,이 같이있는 UITableViewCell의 확장을 작성할 수 있습니다 :
extension UITableViewCell {
func separator(hide: Bool) {
separatorInset.left = hide ? bounds.size.width : 0
}
}
그런 다음 아래와 같이 사용할 수 있습니다 (셀이 셀 인스턴스 인 경우).
cell.separator(hide: false) // Shows separator
cell.separator(hide: true) // Hides separator
테이블 뷰 셀의 너비를 임의의 숫자를 할당하는 대신 왼쪽 삽입으로 할당하는 것이 좋습니다. 일부 화면 크기에서는 현재는 아니지만 나중에는 임의의 숫자로는 충분하지 않기 때문에 구분 기호를 계속 볼 수 있습니다. 또한 가로 모드의 iPad에서는 구분 기호가 항상 보이지 않도록 보장 할 수 없습니다.