viewForFooterInSection

本文介绍如何在 UITableView 的 footer 中添加 UISwitch 和 UILabel,并调整其布局及交互属性,确保组件能正确显示并响应用户操作。

http://stackoverflow.com/questions/838266/how-do-i-add-a-uibutton-or-uiswitch-in-tableviewviewforfooterinsection

 

// Need to refactor so that the label is Public Sharing and Priviate Sharing and the actions work for each switch

- ( UIView *) tableView : ( UITableView *) tableView
viewForFooterInSection
: ( NSInteger ) section
{
 
if ( section == 0 || section == 1 ) {
   
CGRect screenRect = [[ UIScreen mainScreen ] applicationFrame ];
   
UIView * footerView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , screenRect . size . width , 44.0 )];
    footerView
. autoresizesSubviews = YES ;
    footerView
. autoresizingMask = UIViewAutoresizingFlexibleWidth ;
    footerView
. userInteractionEnabled = YES ;

    footerView
. hidden = NO ;
    footerView
. multipleTouchEnabled = NO ;
    footerView
. opaque = NO ;
    footerView
. contentMode = UIViewContentModeScaleToFill ;

   
// Add the label
   
UILabel *    footerLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 150.0 , - 5.0 , 120.0 , 45.0 )];
    footerLabel
. backgroundColor = [ UIColor clearColor ];
    footerLabel
. opaque = NO ;
    footerLabel
. text = @ "Sharing" ;
    footerLabel
. textColor = [ UIColor tableHeaderAndFooterColor ];
    footerLabel
. highlightedTextColor = [ UIColor tableHeaderAndFooterColor ];
    footerLabel
. font = [ UIFont boldSystemFontOfSize : 17 ];
    footerLabel
. shadowColor = [ UIColor whiteColor ];
    footerLabel
. shadowOffset = CGSizeMake ( 0.0 , 1.0 );
   
[ footerView addSubview : footerLabel ];

   
[ footerLabel release ];  

   
// Add the switch
   
UISwitch * footerSwitch = [[ UISwitch alloc ] initWithFrame : CGRectMake ( 215.0 , 5 , 80.0 , 45.0 )];
   
[ footerView addSubview : footerSwitch ];

   
// Return the footerView
   
return footerView ;
 
}
 
else return nil ;
}
// Need to call to pad the footer height otherwise the footer collapses
- ( CGFloat ) tableView :( UITableView *) tableView heightForFooterInSection :( NSInteger ) section {
 
switch ( section ) {
   
case 0 :
     
return 40.0 ;
   
case 1 :
     
return 40.0 ;
   
default :
     
return 0.0 ;
 
}
}
class GeneralListViewPresenter<Delegate: GeneralListViewPresentable, HeaderPresenter: GeneralListViewCellOverallDataSettable, ItemPresenter: GeneralListViewCellOverallDataSettable, FooterPresenter: GeneralListViewCellOverallDataSettable>: NSObject, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource where Delegate.Item == ItemPresenter.T, Delegate.Header == HeaderPresenter.T, Delegate.Footer == FooterPresenter.T { typealias Item = Delegate.Item typealias Header = Delegate.Header typealias Footer = Delegate.Footer typealias SectionData = GeneralListViewSectionData<Header, Item, Footer> weak var delegate: Delegate? var sections: [SectionData] { return delegate?.sectionDataList() ?? [] } private var _savedSections: [SectionData] = [] //MARK: UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { //保障深拷贝? _savedSections = sections return _savedSections.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return _savedSections[section].items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let delegate = self.delegate else { fatalError() } let item = _savedSections[indexPath.section].items[indexPath.row] let identifier = delegate.identifierForItem(item) let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) if let presenter = cell as? ItemPresenter { presenter.apply(item, at: indexPath) } return cell } //MARK: UITableViewDelegate func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { guard let delegate = self.delegate else { fatalError() } guard let item = _savedSections[section].header else { return nil } let identifier = delegate.identifierForHeader(item) let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) if let presenter = header as? HeaderPresenter { presenter.apply(item, at: IndexPath(row: 0, section: section)) } return header } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { guard let delegate = self.delegate else { fatalError() } guard let item = _savedSections[section].footer else { return UIView() } let identifier = delegate.identifierForFooter(item) let footer = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) if let presenter = footer as? FooterPresenter { presenter.apply(item, at: IndexPath(row: 0, section: section)) } return footer } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return UITableView.automaticDimension } //MARK: UICollectionViewDataSource func numberOfSections(in collectionView: UICollectionView) -> Int { _savedSections = sections return _savedSections.count } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return _savedSections[section].items.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let delegate = self.delegate else { fatalError() } let item = _savedSections[indexPath.section].items[indexPath.row] let identifier = delegate.identifierForItem(item) let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) //设备列表大图模式NVR通道收藏/取消收藏 if let newCell = cell as? OnlineChannelCardCell { newCell.collecAction = { (collect, device, channel) in if let ctr = UIViewController.current as? OnlineDeviceListViewController { ctr.collectButtonClicked(isCollect: collect, device: device, channel: channel) } } //设备列表大图模式NVR Channel进入Help页面 newCell.helpAction = { (device, channel) in let storyboard = UIStoryboard(name: "DeviceList", bundle: nil) if let ctr = storyboard.instantiateViewController(withIdentifier: "DeviceOfflineHelpViewController") as? DeviceOfflineHelpViewController, let rootCtr = UIViewController.current as? OnlineDeviceListViewController { ctr.deviceListType = device.listType; ctr.deviceType = device.deviceType ctr.device = device ctr.channelID = TPSSChannelId(channel.channelId) rootCtr.navigationController?.pushViewController(ctr, animated: true); } } } if let presenter = cell as? ItemPresenter { presenter.apply(item, at: indexPath) } return cell } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { if kind == UICollectionView.elementKindSectionHeader { guard let delegate = self.delegate else { fatalError() } guard let item = _savedSections[indexPath.section].header else { fatalError() } let identifier = delegate.identifierForHeader(item) let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) if let presenter = header as? HeaderPresenter { presenter.apply(item, at: indexPath) } return header } else if kind == UICollectionView.elementKindSectionFooter { guard let delegate = self.delegate else { fatalError() } guard let item = _savedSections[indexPath.section].footer else { fatalError() } let identifier = delegate.identifierForFooter(item) let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath) if let presenter = footer as? FooterPresenter { presenter.apply(item, at: indexPath) } return footer } fatalError() } }
最新发布
10-22
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值