iphone - 在 uitableviewcontroller 顶部添加新对象

标签 iphone ios uitableview

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

我的目标是在 TableView Controller 的顶部显示新数据(索引 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;
}

最佳答案

如果您查看仅在第一次创建单元格时将值分配给 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];

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

还将 subview 添加到单元格而不是单元格的内容 View 。

- (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;
}

关于iphone - 在 uitableviewcontroller 顶部添加新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18836499/

相关文章:

ios - 在设备而不是模拟器上运行 sprite-kit 项目时将每秒帧数设置为 30

ios - 如何将 View 置于 StoryBoard 中 tableview 内容 View 的顶部?

iphone - NSLocalizedString显示键值

jquery - iOS 错误?使用 jQuery 聚焦时无法更改选择中的文本

ios - 具有备用文件名的 UIActivityViewController?

iphone - 没有名称或路径为 "iphoneos4.0"的SDK

ios - Apple Watch - 仅在手机上的应用程序处于事件状态时获取数据

ios - 如何重新加载具有不同行数的 UITableView 部分?

ios - 如何将 UITableView 从一个 Controller 克隆到另一个 Controller 以使其保持同步?

objective-c - UITableView 中的自定义单元格在 iOS 13 暗模式下无法正常工作