QMUIStaticTableViewCellDataSource
@interface QMUIStaticTableViewCellDataSource : NSObject
这个控件是为了方便地实现那种类似设置界面的列表(每个 cell 的样式、内容、操作控件均不太一样,每个 cell 之间不复用),使用方式:
- 创建一个带 UITableView 的 viewController。
- 通过 init 或 initWithCellDataSections: 创建一个 dataSource。若通过 init 方法初始化,则请在 tableView 渲染前(viewDidLoad 或更早)手动设置一个 cellDataSections 数组。
- 将第 2 步里的 dataSource 赋值给 tableView.qmui_staticCellDataSource 即可完成一般情况下的界面展示。
- 若需要重写某些 UITableViewDataSource、UITableViewDelegate 方法,则在 viewController 里直接实现该方法,并在方法里调用 QMUIStaticTableViewCellDataSource (Manual) 提供的同名方法即可,具体可参考 QMUI Demo。
-
列表的数据源,是一个二维数组,其中一维表示 section,二维表示某个 section 里的 rows,每次调用这个属性的 setter 方法都会自动刷新 tableView 内容。
Declaration
Objective-C
@property (nonatomic, copy) NSArray<NSArray<QMUIStaticTableViewCellData *> *> *cellDataSections;
Swift
var cellDataSections: [[QMUIStaticTableViewCellData]]! { get set }
-
数据源绑定到的列表,在 UITableView (QMUI_StaticCell) 里会被赋值
Declaration
Objective-C
@property (nonatomic, weak, readonly) UITableView *tableView;
Swift
weak var tableView: UITableView! { get }
-
Undocumented
Declaration
Objective-C
- (instancetype)init NS_DESIGNATED_INITIALIZER;
Swift
init!()
-
Undocumented
Declaration
Objective-C
- (instancetype)initWithCellDataSections:(NSArray<NSArray<QMUIStaticTableViewCellData *> *> *)cellDataSections NS_DESIGNATED_INITIALIZER;
Swift
init!(cellDataSections: [[QMUIStaticTableViewCellData]]!)
-
从 dataSource 里获取处于 indexPath 位置的 QMUIStaticTableViewCellData 对象
Declaration
Objective-C
- (QMUIStaticTableViewCellData *)cellDataAtIndexPath:(NSIndexPath *)indexPath;
Swift
func cellData(at indexPath: IndexPath!) -> QMUIStaticTableViewCellData!
Parameters
indexPath
cell 所处的位置
-
根据 dataSource 计算出指定的 indexPath 的 cell 所对应的 reuseIdentifier(static tableView 里一般每个 cell 的 reuseIdentifier 都是不一样的,避免复用)
Declaration
Objective-C
- (NSString *)reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath;
Swift
func reuseIdentifierForCell(at indexPath: IndexPath!) -> String!
Parameters
indexPath
cell 所处的位置
-
用于结合 indexPath 和 dataSource 生成 cell 的方法,其中 cell 使用的是 QMUITableViewCell @prama indexPath 当前 cell 的 indexPath
Declaration
Objective-C
- (__kindof QMUITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Swift
func cellForRow(at indexPath: IndexPath!) -> QMUITableViewCell!
-
从 dataSource 里获取指定位置的 cell 的高度 @prama indexPath 当前 cell 的 indexPath
Declaration
Objective-C
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Swift
func heightForRow(at indexPath: IndexPath!) -> CGFloat
Return Value
该位置的 cell 的高度
-
在 tableView:didSelectRowAtIndexPath: 里调用,可从 dataSource 里读取对应 indexPath 的 cellData,然后触发其中的 target 和 action
Declaration
Objective-C
- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Swift
func didSelectRow(at indexPath: IndexPath!)
Parameters
indexPath
当前 cell 的 indexPath
-
在 tableView:accessoryButtonTappedForRowWithIndexPath: 里调用,可从 dataSource 里读取对应 indexPath 的 cellData,然后触发其中的 target 和 action
Declaration
Objective-C
- (void)accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
Swift
func accessoryButtonTappedForRow(with indexPath: IndexPath!)
Parameters
indexPath
当前 cell 的 indexPath