programing

UITableView를 그룹화 스타일로 설정하는 방법

nasanasas 2020. 9. 20. 10:02
반응형

UITableView를 그룹화 스타일로 설정하는 방법


UITableViewController섹션 이있는 하위 클래스가 있습니다. 섹션은 기본 스타일 (둥근 모서리 없음)로 표시됩니다. TableView 스타일을 코드에서 그룹화하려면 어떻게해야합니까? 이를 위해 Interface Builder를 사용하지 않으므로 다음과 같은 것이 필요합니다.

[self.tableView setGroupedStyle]

Stack Overflow에서 검색했지만 답변을 얻지 못했습니다.


무슨 뜻인지 이해한다면 그 스타일로 컨트롤러를 초기화해야합니다. 다음과 같은 것 :

myTVContoller = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];

다음을 수행 할 수 있습니다.

UITableView *myTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

스위프트 3 :

let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)

내 솔루션을 제공합니다. "XIB 모드"에서 작업하고 있습니다. 여기에는 UITableViewController의 하위 클래스 코드가 있습니다.

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

아래 코드는 나를 위해 일했으며 UITableview 클래스도 사용하고 있습니다.

- (id)initWithStyle:(UITableViewStyle)style
{
     self = [super initWithStyle:UITableViewStyleGrouped];

     if (self)
    {

     }
    return self;
}

UITableViewController를 상속하는 경우 tableView를 다시 초기화 할 수 있습니다.

목표 C :

self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

스위프트 4 :

self.tableView = UITableView(frame: CGRect.zero, style: .grouped)

질문에서 언급했듯이 설정은 그렇게 어렵지 않습니다. 사실 꽤 간단합니다. 스토리 보드에서 시도해보세요.

여기에 이미지 설명 입력


UI 자체에서 그룹화 된 스타일을 설정하려면 :-TableView를 선택한 다음 "스타일"(속성 검사기에서))을 일반에서 그룹화로 변경합니다.


별도의 신속한 파일로 이미 생성 한 하위 클래스에서 사용하려는 경우에도이 작업을 수행 할 수 있습니다 (아마 100 % 정확하지는 않지만 작동 함).

override init(style: UITableViewStyle) {
    super.init(style: style)
    UITableViewStyle.Grouped
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

이제 appdelegate.swift에서 다음을 호출 할 수 있습니다.

let settingsController = SettingsViewController(style: .Grouped)

스위프트 4

일반 TableView 사용

let tableView = TableView(frame: .zero, style: .grouped)

TPKeyboardAvoidingTableView 사용

let tableView = TPKeyboardAvoidingTableView(frame: .zero, style: .grouped)

Swift 4+ :

let myTableViewController = UITableViewController(style: .grouped)

스토리 보드 / XIB를 사용하여이 작업을 수행 할 수도 있습니다.

  1. 스토리 보드로 이동-> viewController 선택-> 테이블 선택
  2. 인터페이스 작성기에서 "스타일"속성을 선택합니다.
  3. "그룹화"를 선택하십시오.
  4. 끝난

If you have one TableView for more tables, and one of this tables is grouped and the another one plain, than you can simulate the plain style with the function from UITableViewDelegate:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {   
    return CGFloat.min
}

swift 4

if you don't want use storyboard, this might be help.

you can add table view and set properties in a closure:

lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)

        tableView.backgroundColor = UIColor(named: Palette.secondaryLight.rawValue)
        tableView.rowHeight = 68
        tableView.separatorStyle = .none
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()

then add in subview and set constraints.


You can also try to make the separator line color clear which could give the grouped style effect:

[myTVContoller.tableView setSeparatorColor:[UIColor clearColor]];

You can use:

(instancetype)init {
return [[YourSubclassOfTableView alloc] initWithStyle:UITableViewStyleGrouped];
}

self.tableView.style = UITableViewStyleGrouped

EDIT:

Had assumed this was a read/write property. In that case, you can either follow Dimitris advice and set the style when you instantiate the controller, or (if you're using a XIB), you can set it via IB.

참고URL : https://stackoverflow.com/questions/1006663/how-can-i-set-a-uitableview-to-grouped-style

반응형