简体   繁体   English

在uitableviewcontroller顶部添加新对象

[英]add new object at the top of uitableviewcontroller

i am new in ios world, i have some data to be loaded in table view controller and thats load perfectly.. now if a new insert were done the new data is added to a NSMutableArray starting from index 0 (new data is at the top and old data are after them) now after updating the NSMutableArray with the new data i call [tableview reloadData]. 我是ios世界的新手,我要在表视图控制器中加载一些数据,并且可以完美加载..现在,如果完成了新插入,则新数据将从索引0开始添加到NSMutableArray(新数据位于顶部现在使用新数据更新NSMutableArray之后,我调用[tableview reloadData]。

my aim is to show the new data at the top of table view controller (index 0) but nothing of the new data is loaded ! 我的目的是在表视图控制器(索引0)的顶部显示新数据,但是什么也没有加载! why ? 为什么呢?

this is the code i use to load table cell, where data is the NSMutableArray containing my objects : 这是我用来加载表格单元格的代码,其中数据是包含我的对象的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;
}

If you look at the code your putting in your only assigning the value to the UILabel the first time the cell is created, on any dequeue methods where cell != nil then you just change the font and color for the label already in the cell since your using StyleValue1 如果您看一下代码,则在第一次创建单元格时仅将值分配给UILabel,在单元格!= 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];

You do not need this if you just use the label that is associated already with the cell, otherwise if you do need this then you should give the label tag so that you can retrieve it again from the cell when you go to display. 如果仅使用已经与该单元格关联的标签,则不需要此标签;否则,如果您确实需要此标签,则应给该标签标签,以便在显示时可以再次从该单元格中检索它。

Also your adding the subview to the cell instead of the content view of the cell. 您还可以将子视图添加到单元格,而不是单元格的内容视图。

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

{ UILabel *text; {UILabel * text; NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row]; 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;
}

I think you have a misunderstanding: A table view cell is normally an object of a certain structure (say, a title and a subtitle) that is identified by an identifier. 我认为您有一个误解:表格视图单元通常是由标识符标识的具有特定结构(例如标题和副标题)的对象。 So, in the simplest case, all cells of a table view have the same structure and thus the same identifier. 因此,在最简单的情况下,表视图的所有单元格都具有相同的结构,因此具有相同的标识符。 The reason is that a table view cell can be re-used: If a cell is scrolled out of the screen, it is put back into a pool, and when a new cell is scrolled into the screen, a cell of the pool is used to display new data. 原因是可以重新使用表格视图单元格:如果将单元格滚动出屏幕,则将其放回池中,而当新单元格滚动到屏幕中时,将使用池中的单元格显示新数据。 In this case it make no sense at all to create a new identifier for each row of the table. 在这种情况下,为表的每一行创建一个新的标识符根本没有意义。
You problem might be that you ask the table view for a cell with an unknown identifier, which you create dynamically using NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row]; 您的问题可能是您向表视图询问了具有未知标识符的单元格,该单元格是使用NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];动态创建的NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
I suggest using a single table view cell type, identified by a single identifier, and setting its property cell.textLabel.text accordingly. 我建议使用由单个标识符标识的单一表格视图单元格类型,并相应地设置其属性cell.textLabel.text

Try the following things: 请尝试以下操作:

1.Change your code to below , it should work: 1.将代码更改为以下代码,它应该可以工作:

move your text UILabel as a iVar in the MyCell and add it there to the cellView. 将文本UILabel作为iVar移动到MyCell中,然后将其添加到cellView中。 Just configure the information alone based on different index outside the initialization block. 只需根据初始化块外部的不同索引单独配置信息。

(It is a good practice to move static style methods to the initialization phase itself. ) (将静态样式方法移至初始化阶段本身是一个好习惯。)

    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. Also check if your properly removing the previous items from your mutable array. 还要检查您是否从可变数组中正确删除了之前的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM