多表视图部分:第一部分带有静态单元格,第二部分带有动态数量的单元格 [英] Multiple table view section: first section with static cell and second section with dynamic number of cell

查看:113
本文介绍了多表视图部分:第一部分带有静态单元格,第二部分带有动态数量的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想创建一个包含两个部分的表格视图。第一秒只包含一个带文本框的单元格。第二部分应该用数组的内容动态填充。

As the title says, I would like to create a table view with two sections. The first second just contains one cell with a textbox. The second section should be filled dynamically with the content of an array.

我写了这段代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        static NSString *simpleTableIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

        Player *player = [self.playerController getPlayerAtPositon:indexPath.row];

        cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];

        return cell;
    }
    else
    {
       // ???
    }
}

但是我必须写入 else codeblock?那么我填充文本框的单元格不会被覆盖?

But what do I have to write into the else codeblock? So that the cell I filld with a textbox will not be overwrite?

我发现,你不能只是动态添加元素。如果要添加三个播放器,还必须将r​​ows-value设置为三。否则应用程序将崩溃。有没有办法动态地做到这一点?

I found out, that you can't just add elements dynamically. When you want to add, for example, three players, you also have to set the rows-value to three. Otherwise the app will crash. Is there a way to do this dynamically?

推荐答案

你有两种类型的单元格,一种是有文本字段,第二种是有玩家信息。因此,您应为每种类型的单元格创建不同的单元格标识符。

You have 2 types of cell one that has textfield, and second that has player info. So you should create a different cell identifier for each kind of cell.

    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";

    if(indexpath.section == 0) {
         // create and return a cell with CellIdentifier1 
    }
    else {
         // create and return a cell with CellIdentifier2  
    }

还有一件事,当allocateg UITableViewcell不要忘记最后添加自动释放...
希望这会对你有帮助....

One more thing while allocationg UITableViewcell don't forget to add autorelease at the end ... Hope this will help you....

这篇关于多表视图部分:第一部分带有静态单元格,第二部分带有动态数量的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆