如何在一个单元格(tableView)中显示两个对象?
我遇到了一个小问题。我创建了一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不仅会在不同的单元格中显示名字和姓氏,而且对于索引大于 1 的行也不起作用。这是因为您的表数据源从您在第一行使用 alloc/init 创建的全新数组中读取,而不是从您的模型中读取。你的小数组只包含两个对象,解释这两行。
您需要执行以下操作:首先,使一组用户可用于表的数据源。假设它名为
userArray
。将每个用户的用户数据放入该数组中。假设每个用户都存储为MYUser
类型的对象,该对象响应[user firstName]
和[user lastName]
调用,其中第一个和用户的姓氏。现在将此代码放入表的数据源中:
编辑:如果您的
menuArray
将名字和姓氏存储在单独的连续字符串对象中,您仍然可以这样做(尽管我建议不要这样做,因为这会让将来维护你的程序的人感到困惑)。首先,您需要将返回表中项目数的方法更改为返回
menuArray.count / 2
,而不仅仅是menuArray.count
。然后您可以按如下方式修改代码:EDIT2 :以下是创建一个存储用户名字和姓氏的类的方法。您将
@interface
部分添加到MYUser.h
文件中,并将@implementation
部分添加到MYUser.m
文件中。然后,您可以在引用MYUser
类的 .m 文件中导入MYUser.h
,并使用initWithFirstName:andLastName:
初始化新实例。下面的示例假设您使用 ARC:MYUser.h 文件:
MYUser.m 文件:
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 typeMYUser
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:
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 justmenuArray.count
. Then you can modify your code as follows:EDIT2 : Here is how you create a class that stores the first and the last name of a user. You add the
@interface
part toMYUser.h
, and the@implementation
part toMYUser.m
file. You then importMYUser.h
in .m files from which you referenceMYUser
class, and useinitWithFirstName:andLastName:
to initialize new instances. The sample below assumes that you use ARC:MYUser.h file:
MYUser.m file: