如何在一个单元格(tableView)中显示两个对象?

发布于 2024-12-28 11:08:09 字数 759 浏览 2 评论 0原文

我遇到了一个小问题。我创建了一个 UITableView,用户可以在其中添加地址簿中的联系人。 FirstName 显示在第一行,lastName 显示在第二行。我知道问题出在哪里,因为现在我实际上告诉他该怎么做。 但我怎样才能告诉他在同一个单元格中显示两个名字呢?我用以下方法做到了,但它们不起作用。

-(void)addObjectsFromArray:(NSArray *)otherArray
{
     NSMutableArray *myArray = [[NSMutableArray alloc] init];
     [myArray addObjectsFromArray:menuArray];
     NSLog(@"%@", myArray);
     [self.tableView reloadData];
}

问题是我无法显示 myArray 中的对象,因为 cellforRowAtIndex 方法未获取该值。有了这个,我得到了相同的结果(firstName 第一行,lastName 第二行):

NSArray *objects = [[NSArray alloc] initWithObjects:firstName, lastName, nil];
NSString *cellValue = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
NSLog(@"%@", objects);

return cell;

I ran into a small problem. I created a UITableView where the user can add his contacts from his addressbook. The firstName gets displayed in the first row, the lastName in the second one. I know where the problem is, because right now I actually tell him how to do that.
But how can I tell him to display both names in the same cell? I did it with the following methods but they don't work.

-(void)addObjectsFromArray:(NSArray *)otherArray
{
     NSMutableArray *myArray = [[NSMutableArray alloc] init];
     [myArray addObjectsFromArray:menuArray];
     NSLog(@"%@", myArray);
     [self.tableView reloadData];
}

The problem is that I cannot display objects from myArray because cellforRowAtIndex method does not get the value. And with that one I get the same result (firstName first Row, lastName second Row):

NSArray *objects = [[NSArray alloc] initWithObjects:firstName, lastName, nil];
NSString *cellValue = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
NSLog(@"%@", objects);

return cell;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2025-01-04 11:08:09

您的代码不仅会在不同的单元格中显示名字和姓氏,而且对于索引大于 1 的行也不起作用。这是因为您的表数据源从您在第一行使用 alloc/init 创建的全新数组中读取,而不是从您的模型中读取。你的小数组只包含两个对象,解释这两行。

您需要执行以下操作:首先,使一组用户可用于表的数据源。假设它名为userArray。将每个用户的用户数据放入该数组中。假设每个用户都存储为 MYUser 类型的对象,该对象响应 [user firstName][user lastName] 调用,其中第一个和用户的姓氏。

现在将此代码放入表的数据源中:

MYUser *user = [userArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
cell.textLabel.text = cellValue;

编辑:如果您的menuArray将名字和姓氏存储在单独的连续字符串对象中,您仍然可以这样做(尽管我建议不要这样做,因为这会让将来维护你的程序的人感到困惑)。

首先,您需要将返回表中项目数的方法更改为返回 menuArray.count / 2,而不仅仅是 menuArray.count。然后您可以按如下方式修改代码:

NSString firstName = [menuArray objectAtIndex:indexPath.row/2];
NSString lastName = [menuArray objectAtIndex:indexPath.row/2 + 1];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
cell.textLabel.text = cellValue;

EDIT2 :以下是创建一个存储用户名字和姓氏的类的方法。您将 @interface 部分添加到 MYUser.h 文件中,并将 @implementation 部分添加到 MYUser.m 文件中。然后,您可以在引用 MYUser 类的 .m 文件中导入 MYUser.h,并使用 initWithFirstName:andLastName: 初始化新实例。下面的示例假设您使用 ARC:

MYUser.h 文件:

#import <UIKit/UIKit.h>
@interface MYUser : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last;
@end

MYUser.m 文件:

@implementation MYUser
@synthesize firstName, lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last {
    self = [super init];
    if (self) {
        self.firstName = first;
        self.lastName = last;
    }
    return self;
}
@end

Not only does your code display the first name and last name in different cells, it also does not work for rows at indexes greater than one. This is because your table data source reads from a brand-new array that you create with alloc/init on the first line, rather than reading from your model. Your little array contains only two objects, explaining the two rows.

Here is what you need to do: first, make an array of users available to your table's data source. Suppose it's called userArray. Put user data for each user into that array. Suppose each user is stored as an object of type MYUser that responds to [user firstName] and [user lastName] calls with the first and the last name of the user.

Now put this code into your table's data source:

MYUser *user = [userArray objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", [user firstName], [user lastName]];
cell.textLabel.text = cellValue;

EDIT : If your menuArray stores the first and the last names in separate consecutive string objects, you can still do it (though I recommend against it, because it will be confusing to people who maintain your program in the future).

First, you need to change the method that returns the number of items in the table to return menuArray.count / 2 instead of just menuArray.count. Then you can modify your code as follows:

NSString firstName = [menuArray objectAtIndex:indexPath.row/2];
NSString lastName = [menuArray objectAtIndex:indexPath.row/2 + 1];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
cell.textLabel.text = cellValue;

EDIT2 : Here is how you create a class that stores the first and the last name of a user. You add the @interface part to MYUser.h, and the @implementation part to MYUser.m file. You then import MYUser.h in .m files from which you reference MYUser class, and use initWithFirstName:andLastName: to initialize new instances. The sample below assumes that you use ARC:

MYUser.h file:

#import <UIKit/UIKit.h>
@interface MYUser : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last;
@end

MYUser.m file:

@implementation MYUser
@synthesize firstName, lastName;
-(id)initWithFirstName:(NSString*)first andLastName:(NSString*)last {
    self = [super init];
    if (self) {
        self.firstName = first;
        self.lastName = last;
    }
    return self;
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文