更改 id 对象的描述
Changing the description of an id object
我将项目存储在一个数组中以存储在 myTableView 中,如下所示:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[nameField becomeFirstResponder];
[self.objects insertObject:@"New Friend" atIndex:0];
//[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
一旦创建了对象,它就会作为 "New Friend" 存储到 table 视图中。创建对象后,我可以使用什么方法将 "New Friend" 更改为其他内容?
如果有类似的方法,像下面这样的东西是理想的:
[self.detailItem changeDescription:string1]
我认为您对代码的作用有很深的误解。 "The object" 不是某个未指定类型的对象,其 描述 是 "New Friend"。您实际上已经将值为 "New Friend" 的 NSString
实例插入到索引 0.
的数组中
您无法更改 NSString
实例的值。是immutable。您可以将索引 0 处的对象替换为不同的字符串对象,但我认为这仍然不是您的意思。
您可能希望 table 视图代表某种更有意义的对象,例如 Person
对象。如果是这样,那么您需要声明和定义这样一个 Person
class,可能具有 name
、age
等属性。然后,您将创建此实例Person
class 并将其存储在您的数组中(将显示在 table 视图中)。稍后,如果需要,您可以更改此类实例的 name
属性.
的值
我将项目存储在一个数组中以存储在 myTableView 中,如下所示:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[nameField becomeFirstResponder];
[self.objects insertObject:@"New Friend" atIndex:0];
//[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
一旦创建了对象,它就会作为 "New Friend" 存储到 table 视图中。创建对象后,我可以使用什么方法将 "New Friend" 更改为其他内容?
如果有类似的方法,像下面这样的东西是理想的:
[self.detailItem changeDescription:string1]
我认为您对代码的作用有很深的误解。 "The object" 不是某个未指定类型的对象,其 描述 是 "New Friend"。您实际上已经将值为 "New Friend" 的 NSString
实例插入到索引 0.
您无法更改 NSString
实例的值。是immutable。您可以将索引 0 处的对象替换为不同的字符串对象,但我认为这仍然不是您的意思。
您可能希望 table 视图代表某种更有意义的对象,例如 Person
对象。如果是这样,那么您需要声明和定义这样一个 Person
class,可能具有 name
、age
等属性。然后,您将创建此实例Person
class 并将其存储在您的数组中(将显示在 table 视图中)。稍后,如果需要,您可以更改此类实例的 name
属性.