0

我正在尝试分配标签,然后创建一个NSMutableArray

#import "myClass"

static NSString *kC = @"100";
static NSString *kLo = @"110";

@interface MyApp()
@property (strong, nonatomic) NSMutableArray *arrayTag;
@end

@Synthesize arrayTag;

viewDidLoad

arrayTag = [[NSMutableArray alloc] init];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.showsReorderControl = YES;
    }

    if (indexPath.row == 0)
    {
        cell.tag = kC;
            NSString *cTag = [NSString stringWithFormat:@"%i", cell.tag];
            if (![arrayTag containsObject:cTag])
            {
                [arrayTag addObject:cTag];
            }
    }

    if (indexPath.row == 1)
    {
         cell.tag = kLo;
            NSString *loTag = [NSString stringWithFormat:@"%i", cell.tag];
            if (![arrayTag containsObject:loTag])
            {
                [arrayTag addObject:loTag];
            }
    }
return cell;
}

NSLog

- (IBAction)doneButton:(id)sender
{

    NSLog (@"Number of Objects in Array %i", arrayTag.count);

    NSLog (@"Object at Index 0 in Array %@", [arrayTag objectAtIndex:0]);
    NSLog (@"Object at Index 1 in Array %@", [arrayTag objectAtIndex:1]);

    for (NSString *obj in arrayTag){
        NSLog(@"From ArrayTag obj: %@", obj);
    }
}

这是日志

2013-07-10 15:46:39.468 MyApp[1013:c07] Number of Objects in Array 2
2013-07-10 15:46:39.469 MyApp[1013:c07] Object at Index 0 in Array 1624272
2013-07-10 15:46:39.469 MyApp[1013:c07] Object at Index 1 in Array 1624256
2013-07-10 15:46:39.469 MyApp[1013:c07] From ArrayTag obj: 1624272
2013-07-10 15:46:39.470 MyApp[1013:c07] From ArrayTag obj: 1624256

Object[0] = 100问: and的值不应该Object[1] = 110匹配cell.tag吗?为什么会显示 1624272 和 1624256?

4

1 回答 1

3

UIView 的tag属性需要一个NSInteger,您正在分配一个NSString,或者更准确地说,分配给NSString标签的内存地址。

使用这样的东西:

static NSInteger kC = 100;
static NSInteger kLo = 110;
于 2013-07-10T23:04:36.880 回答