xcode addObject: 到 NSMutableArray 并发送到 TableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8826028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addObject: to NSMutableArray and sending to TableView
提问by Solid I
Xcode 4.2 iPhone Project
Xcode 4.2 iPhone 项目
I am trying to create a tableView that's populated from an NSMutableArray. Currently when I run the simulator the tableView in question shows 5 rows (this is correct) but all rows have the same string (this obviously is not correct). The string being shown is from the LAST object defined in my NSMutableArray. Here's the code:
我正在尝试创建一个从 NSMutableArray 填充的 tableView。目前,当我运行模拟器时,有问题的 tableView 显示 5 行(这是正确的),但所有行都具有相同的字符串(这显然不正确)。显示的字符串来自在我的 NSMutableArray 中定义的 LAST 对象。这是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
codes = [[NSMutableArray alloc] init];
TableList *listItem = [[TableList alloc] init];
[listItem setCodeTitle:@"Test Name 1"];
[listItem setCodeNumber:@"101"];
[listItem setCodeDescription:@"This is the description for 101."];
[codes addObject:listItem];
[listItem setCodeTitle:@"Test Name 2"];
[listItem setCodeNumber:@"102"];
[listItem setCodeDescription:@"This is the description for 102."];
[codes addObject:listItem];
[listItem setCodeTitle:@"Test Name 3"];
[listItem setCodeNumber:@"103"];
[listItem setCodeDescription:@"This is the description for 103."];
[codes addObject:listItem];
[listItem setCodeTitle:@"Test Name 4"];
[listItem setCodeNumber:@"104"];
[listItem setCodeDescription:@"This is the description for 104."];
[codes addObject:listItem];
[listItem setCodeTitle:@"Test Name 5"];
[listItem setCodeNumber:@"105"];
[listItem setCodeDescription:@"This is the description for 105."];
[codes addObject:listItem];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [codes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CodeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
TableList *current = [codes objectAtIndex:indexPath.row];
cell.textLabel.text = [current codeTitle];
return cell;
}
I hope this is painfully obvious to anyone with more experience than me... I have very little.
我希望这对于比我更有经验的人来说是显而易见的……我的经验很少。
采纳答案by Lily Ballard
You're storing the exact same object 5 times into your array. You seem to be under the misapprehension that -addObject:
will copy the object it's handed, but it won't. The simple fix is to change your -addObject:
lines to say
您将完全相同的对象存储 5 次到您的数组中。您似乎误以为-addObject:
会复制它递过来的对象,但事实并非如此。简单的解决方法是改变你的-addObject:
台词说
[codes addObject:[[listItem copy] autorelease]];
This is, of course, assuming your TableList
class conforms to <NSCopying>
. If it doesn't, you'll probably want to do so. If you're unwilling to support copying in that class, then you'll have to create a new TableList
object each time.
当然,这是假设您的TableList
课程符合<NSCopying>
. 如果没有,您可能想要这样做。如果您不愿意在该类中支持复制,那么您每次都必须创建一个新TableList
对象。
Also, unless you're using ARC, you're leaking your TableList
object.
此外,除非您使用 ARC,否则您正在泄漏您的TableList
对象。
In order to support -copy
, you must conform to <NSCopying>
, which is a protocol that declares the method -copyWithZone:
(though the zone argument is a historical curiosity that should be ignored). To do this, modify your @interface
line to look like
为了支持-copy
,您必须遵守<NSCopying>
,这是一个声明方法的协议-copyWithZone:
(尽管 zone 参数是历史上的好奇,应该被忽略)。要做到这一点,修改你的@interface
行看起来像
@interface TableList : NSObject <NSCopying>
and then implement the method -copyWithZone:
然后实现方法 -copyWithZone:
- (id)copyWithZone:(NSZone *)zone {
// If our superclass conformed to <NSCopying> we'd call [super copy]
// But it doesn't, so alloc/init a new instance
TableList *newObj = [[TableList alloc] init];
newObj.codeTitle = self.codeTitle;
newObj.codeNumber = self.codeNumber;
newObj.codeDescription = self.codeDescription;
return newObj;
}
This is just a sample implementation. It assumes that those 3 properties are the only values on the object, and that simply assigning to the property is appropriate. If you have internal ivars that need to be copied over, you can get ivar access by using something like
这只是一个示例实现。它假设这 3 个属性是对象上的唯一值,并且简单地分配给属性是合适的。如果您有需要复制的内部 ivars,您可以使用类似的方法获得 ivar 访问权限
newObj->ivar = copyIvar(self->ivar); // self->ivar is the same as just plain ivar
回答by verma
Also, if you are using NSMutableArray, try chaining it to an NSArray.
此外,如果您使用 NSMutableArray,请尝试将其链接到 NSArray。