在表视图中组合静态和原型内容

taz*_*boy 36 iphone storyboard uitableview ios

有没有办法使用故事板将静态tableview单元格(静态内容)与动态tableview单元格(原型内容)结合起来?

dan*_*anh 56

我建议你把你的桌子视为动态的,但是在顶部包含你总是想要的细胞.在故事板中,放置一个UITableViewController并使用动态表.根据UITableViewCell需要在表格中添加尽可能多的原型.比方说,一个用于静态单元,一个用于表示可变单元.

在你的UITableViewDataSource班上:

#define NUMBER_OF_STATIC_CELLS  3

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dynamicModel count] + NUMBER_OF_STATIC_CELLS;
}
Run Code Online (Sandbox Code Playgroud)

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < NUMBER_OF_STATIC_CELLS) {
        // dequeue and configure my static cell for indexPath.row
        NSString *cellIdentifier = ... // id for one of my static cells
    } else {
        // normal dynamic logic here
        NSString *cellIdentifier = @"DynamicCellID"
        // dequeue and configure for [self.myDynamicModel objectAtIndex:indexPath.row]
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您将不同的部分用于固定长度和动态行,则可以避免大部分不快乐.UI可以在这些之间保持相同,不使用标题相同的单元格类型等. (2认同)

Abu*_*air 12

我有一个问题,虽然这是一个轻微的变种.我实际上想要混合动态和静态细胞,但是在不同的组中.意义组1将具有仅静态单元格,而组2将具有动态单元格.

我通过实际硬编码静态单元格值(基于它们的原型单元标识符)来实现这一点.动态部分将具有正常的动态填充内容.以下是一些示例代码,以防其他人遇到同样的问题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{    
    if (section == 1){
        return @"Dynamic Cells";
    }
    if (section == 0){
        return @"Static Cells";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1; //However many static cells you want
    } else {
        return [_yourArray count];
    }
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {
        NSString *cellIdentifier = @"staticCellType";   
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = @"some static content";        
        return cell;

    } else if (indexPath.section == 1){

        NSString *cellIdentifier = @"dynamicCellType";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row];
        return cell;

    } 
    return nil;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
Run Code Online (Sandbox Code Playgroud)

  • 这比上面的答案更好.静态和动态内容可以在不同的部分.谢谢. (2认同)

Sea*_*ean 5

由于没有人真正提供问题的真实答案(在同一个表视图中同时使用静态和原型单元格),我想我会插话.可以做到!

根据需要创建静态单元格.对于需要动态单元格的部分,如果您不使用标准的UITableViewCell类型,则需要在单独的Nib中创建自定义的部分,否则可以使用标准的Nib.然后实现以下代理.基本上对于每个代表,对于我们想要调用super的静态东西,对于动态,我们返回我们的值.

首先,如果您需要有选择地显示您的动态部分,您将需要实现numberOfSectionsInTableView(否则您可以将此委托留出):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int staticSections = 1;
    int dynamicSections = 1;
    if (SOME_BOOLEAN) {
        return staticSections + dynamicSections;
    } else {
        return staticSections;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要实现numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return A_COUNT;
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要实现heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 44.0f;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}
Run Code Online (Sandbox Code Playgroud)

然后indentationLevelForRowAtIndexPath:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 1; // or manually set in IB (Storyboard)
    } else {
        return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath]; // or 0
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        SomeObject *obj = self.someArray[indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCell" forIndexPath:indexPath];
        cell.textLabel.text = obj.textValue;
        return cell;
    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}
Run Code Online (Sandbox Code Playgroud)