在uitableviewcontroller的顶部添加新对象

时间:2013-09-16 20:13:04

标签: iphone ios uitableview

我是ios世界的新手,我有一些数据要加载到表视图控制器中,那就完全加载了..现在如果新插入完成,新数据将从索引0开始添加到NSMutableArray(新数据是现在,在使用新数据更新NSMutableArray后,我将调用[tableview reloadData]。

我的目标是在表视图控制器(索引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;
}

3 个答案:

答案 0 :(得分:1)

如果你看看你在第一次创建单元格时只将值赋给UILabel的代码,那么在任何dequeue方法中,单元格!= 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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试以下方法:

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. 同时检查您是否正确删除了可变数组中的先前项目。