[英]How to story multiple values for one object in NSMutableDictionary?
I would like to story many items in one object (the name of the user, their photo, and the time the photo was taken). 我想讲述一个对象中的许多项目(用户的名称,他们的照片以及照片的拍摄时间)。 I have tried to use three different arrays to do this and it somewhat works, however since I am loading this into a tableview its not the best way to do this. 我尝试使用三个不同的数组来执行此操作,并且它有些起作用,但是由于将其加载到表视图中,所以这不是最佳方法。 I am thinking about using a NSMutableDictionary but I can't seem to do it. 我正在考虑使用NSMutableDictionary,但我似乎做不到。 Any help would be appreciated! 任何帮助,将不胜感激!
What you want to do is create a User Entity (Swift/Objective-C Class) with username, photo and photoTime fields. 您要执行的操作是使用用户名,照片和照片时间字段创建用户实体(Swift / Objective-C类)。
Create the users, put drop them in an array and then read from it while you are creating the table in cellForRowAtIndexPath
. 创建用户,将其放入数组中,然后在cellForRowAtIndexPath
中创建表时从中读取。
@interface User : NSObject
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSString *photoURL;
@property (nonatomic, retain) NSString *photoTime;
@end
Then somewhere within your code where you create users... 然后在代码中的某个位置创建用户...
NSMutableArray *usersArray= [[NSMutableArray alloc] init];
User *user= [[Useralloc] init];
user.userName=@"someUserName"
user.photoURL=@"http://somephotourl.com";
user.photoTime=@"1231232132121";
[usersArray addObject:user];
You can do like this. 你可以这样
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:
@"Jone",@"UserName",
@"Jone.png",@"Photo",
[NSDate date],@"Time",nil];
NSMutableArray *userArray = [[NSMutableArray alloc] init];
[userArray addObject:dic];
//NSLog(@"%@",userArray);
for (int i=0; i<userArray.count; i++) {
NSDictionary *userDictionary = [userArray objectAtIndex:i];
NSLog(@"UserName:%@",[userDictionary valueForKey:@"UserName"]);
NSLog(@"Photo:%@",[userDictionary valueForKey:@"Photo"]);
NSLog(@"Time:%@",[userDictionary valueForKey:@"Time"]);
}
Note:I think you need to use "NSMutableArray" of NSDictionary. 注意:我认为您需要使用NSDictionary的“ NSMutableArray”。 if you all user property (username,photo,time...) are unpredictable the only you need to use NSMutableDictionary. 如果所有用户属性(用户名,照片,时间...)都是不可预测的,则只需要使用NSMutableDictionary。
The data structure behind a UITableView
is usually NSArray
or a list of some kind. UITableView
背后的数据结构通常是NSArray
或某种列表。 Each object in the array should represent a row in the table. 数组中的每个对象应代表表中的一行。
Following this pattern your structure should look like that: 按照这种模式,您的结构应如下所示:
var users = [User]()
struct User {
let name: String
let photo: UIImage
let date: NSDate
}
// This struct could also be a dictionary
var user = [String: AnyObject]()
user["name"] = ..
user["photo"] = ..
user["date"] = ...
Instantiate User
objects and add them to the users
array when appropriate. 实例化User
对象,并在适当时将其添加到users
数组。
Your UITableView
should find the correct user and update the cell accordingly in the dataSource
method: 您的UITableView
应该找到正确的用户,并在dataSource
方法中相应地更新单元格:
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = .. // Dequeue cell
let user = users[indexPath.row] // Get the user for this row
cell.textLabel.title = user.name // Update cell properties
// etc..
// OR using a dictionary
cell.textLabel.title = user["name"]
// etc..
return cell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.