2016-06-23 5 views
0

Im используя контроллер представления таблицы с заголовками для каждого из моих трех разделов. Я хочу иметь возможность изменить внешний вид заголовков, что было бы лучшим способом сделать это.как я могу отредактировать заголовок раздела таблицы ios swift

что-то похожее на то, как Snapchat историй колонтитулы выглядеть

+0

Опубликовать больше деталей, код или скриншот. – Lumialxk

ответ

4

Вы можете настроить заголовок раздела, настроив UITableViewCell в раскадровке.

Вы проектируете 3-ю секцию так же, как и снимок в раскадровке.

Вот пример двух раздела двух различных UITableView клеток я

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {  
//Say 2 section with two different look 
if section == 0{ 
    let header = tableView.dequeueReusableCellWithIdentifier("HeaderTableViewCell1")! as! HeaderTableViewCell1 
    header._lblGroupName.text = "" 
    header._btnExpand.addTarget(self, action: "hideSection:", forControlEvents: .TouchUpInside) 

    header._lblTotalCount.text = "" 
    return header.contentView 
} 
else{ 
    let header = tableView.dequeueReusableCellWithIdentifier("HeaderTableViewCell2")! as! HeaderTableViewCell2 
    header._lblGroupName.text = "" 
    header._btnExpand.addTarget(self, action: "hideSection:", forControlEvents: .TouchUpInside) 

    header._lblTotalCount.text = "" 
    return header.contentView 
} 
} 

Создайте два пользовательских UITableViewCell классов. Установите розетку

class HeaderTableViewCell1: UITableViewCell { 

@IBOutlet weak var _lblTotalCount: UILabel! 
@IBOutlet weak var _btnExpand: UIButton! 
@IBOutlet weak var _lblGroupName: UILabel! 

} 

class HeaderTableViewCell2: UITableViewCell { 

@IBOutlet weak var _lblTotalCount: UILabel! 
@IBOutlet weak var _btnExpand: UIButton! 
@IBOutlet weak var _lblGroupName: UILabel! 

} 
+1

. Я не уверен, что подразумевается при использовании ячеек для этого, но я думаю, что вы должны быть подклассифицированы' UITableViewHeaderFooterView '. И 'UITableView' имеет функцию' dequeueReusableHeaderFooterView (withIdentifier:) '. – Connor

0

сделать пользовательское представление для вашего заголовка, зарегистрировать его либо:

- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier; 

- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier; 

и настроить его в:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section; 

UPD :

Быстрые версии:

Зарегистрируйте перо в viewDidLoad:

override func viewDidLoad() { 
     super.viewDidLoad() 
    tableView.registerNib(UINib(nibName: "YourNibName", bundle:nil),    forHeaderFooterViewReuseIdentifier: "YourIdentifier") 
    } 

Реализовать UITableViewDelegate метод:

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 

и настроить view там

+0

благодарю вас за ответ im, который очень тяжело разбирается в этом - в любом случае вы можете писать в swift – sauly

1

Просто добавьте это и сделать ваши необходимые настройки

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.sectionHeaderHeight)) 
    // Do your customization 

    return view 
} 
+0

Примечание: вы должны подклассифицировать UITableViewHeaderFooterView и вернуть это. Я бы тоже не стал использовать фрейм. Вы должны указать высоту как константу в 'tableView.sectionHeaderHeight' или реализовать' tableView (_: heightForHeaderInSection:) ' – Jason

0
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell 
    headerCell.backgroundColor = UIColor.cyanColor() 

    switch (section) { 
    case 0: 
     // Do your customization 
     //return sectionHeaderView 
    case 1: 
     // Do your customization 
     //return sectionHeaderView 
    case 2: 
     // Do your customization 
     //return sectionHeaderView 
    default: 
     //return sectionHeaderView 
    } 

    return headerCell 
    } 


override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) 
    footerView.backgroundColor = UIColor.blackColor() 

    return footerView 
    } 
Смежные вопросы