【问题标题】:add new object at the top of uitableviewcontroller在 uitableviewcontroller 顶部添加新对象
【发布时间】:2013-09-16 20:13:04
【问题描述】:

我是 ios 世界的新手,我有一些数据要加载到表视图控制器中,并且加载完美.. 现在,如果完成新插入,则新数据将从索引 0 开始添加到 NSMutableArray(新数据是在顶部,旧数据在它们之后)现在在用我称为 [tableview reloadData] 的新数据更新 NSMutableArray 之后。

我的目标是在表格视图控制器(索引 0)的顶部显示新数据,但没有加载任何新数据!为什么?

这是我用来加载表格单元格的代码,其中数据是包含我的对象的 NSMutableArray:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];

    MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];


        // add description
        UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
        text.numberOfLines=3;
        text.text= [[data objectAtIndex:indexPath.row] desc];
        text.backgroundColor=[UIColor clearColor];

        text.textAlignment= UITextAlignmentRight;
        text.font = [UIFont fontWithName:@"Helvetica" size:13];
        [cell addSubview:text];
    }

    //use your cell here
    cell.textLabel.textAlignment=UITextAlignmentRight;

    cell.textLabel.numberOfLines =3;

    // cell font
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
    cell.textLabel.textColor=[UIColor blackColor];

    return cell;
}

【问题讨论】:

  • 您能说明如何将新数据添加(或插入)到可变数组中吗?
  • 我使用它完美添加的调试对其进行了测试
  • 我有个主意.. 你认为因为单元格是第一次创建的,然后我们用它不加载的新数据重新加载它!我的意思是当回调 cellForRowAtIndexPath 时,单元格 != nil

标签: iphone ios uitableview


【解决方案1】:

如果您查看您在第一次创建单元格时仅将值分配给 UILabel 的代码,则在 cell != nil 的任何出队方法上,您只需更改标签的字体和颜色自您使用 StyleValue1 以来的单元格

// add description
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
    text.numberOfLines=3;
    text.text= [[data objectAtIndex:indexPath.row] desc];
    text.backgroundColor=[UIColor clearColor];

    text.textAlignment= UITextAlignmentRight;
    text.font = [UIFont fontWithName:@"Helvetica" size:13];
    [cell addSubview:text];

如果您只使用已经与单元格关联的标签,则不需要这个,否则如果您确实需要这个,那么您应该提供标签标签,以便您可以在显示时再次从单元格中检索它.

您还将子视图添加到单元格而不是单元格的内容视图。

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

{ UILabel *文本; NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];

MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];


    // add description
    text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
    text.numberOfLines=3;        
    text.backgroundColor=[UIColor clearColor];
    text.tag = 1000;

    text.textAlignment= UITextAlignmentRight;
    text.font = [UIFont fontWithName:@"Helvetica" size:13];
    [cell addSubview:text];
} else {
    text = (UILabel*)[cell viewForTag:1000];
}

// Here you update the value of text
text.text= [[data objectAtIndex:indexPath.row] desc];

//use your cell here
cell.textLabel.textAlignment=UITextAlignmentRight;

cell.textLabel.numberOfLines =3;

// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];

return cell;
}

【讨论】:

  • 是的,这就是问题所在.. 重新创建单元格时它不会 = nil
  • 我该怎么做?我尝试使用 if (cel != nill) 重复代码,但随后我将单元格相互覆盖
  • 要做的是在单元格 == nil 部分给标签一个标签 text.tag = 1000;然后将文本的设置移到外部,即在 if (cell == nil) 之后,然后如果单元格 != nil 你执行单元格 viewForTag:1000 ,这将为你提供已经创建的带有所有默认值的标签,那么你可以做你还需要做的事
  • 但在第一次数据加载时运行会进入 (cell != nil) 的情况!
  • 正确,所以设置该实例中的所有内容都应该是 cell == nil 然后一旦设置好,您就可以在下次出现该单元格时使用该标签,以便您可以将正确的信息放入其中。
【解决方案2】:

我认为您有一个误解:表格视图单元格通常是由标识符标识的特定结构(例如标题和副标题)的对象。因此,在最简单的情况下,表格视图的所有单元格都具有相同的结构,因此具有相同的标识符。原因是表格视图单元格可以重复使用:如果一个单元格滚动出屏幕,则将其放回池中,而当新单元格滚动进入屏幕时,将使用池中的一个单元格显示新数据。在这种情况下,为表的每一行创建一个新标识符根本没有意义。
您的问题可能是您向表格视图询问具有未知标识符的单元格,您使用 NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
动态创建该单元格 我建议使用由单个标识符标识的单一表格视图单元格类型,并相应地设置其属性cell.textLabel.text

【讨论】:

  • 我确实需要标签,因为我有其他自定义没有显示在代码中
【解决方案3】:

尝试以下方法:

1.将您的代码更改为以下,它应该可以工作:

将您的文本 UILabel 作为 iVar 移动到 MyCell 中,并将其添加到 cellView 中。只需根据初始化块外的不同索引单独配置信息即可。

(将静态样式方法移到初始化阶段本身是一个很好的做法。)

    MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];



        cell.text.numberOfLines=3;
        text.backgroundColor=[UIColor clearColor];

        cell.text.textAlignment= UITextAlignmentRight;
        cell.text.font = [UIFont fontWithName:@"Helvetica" size:13];
       // [cell addSubview:text];
     cell.textLabel.textAlignment=UITextAlignmentRight;

    cell.textLabel.numberOfLines =3;

    // cell font
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
    cell.textLabel.textColor=[UIColor blackColor];
    }


 cell.text.text= [[data objectAtIndex:indexPath.row] desc];
    //use your cell here
  1. 还要检查您是否正确地从可变数组中删除了以前的项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多