簡體   English   中英

NSMutableArray無法正確添加對象?

[英]NSMutableArray not adding object correctly?

我有一個按鈕,可以在ButtonClick上保存單元格的數據

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [self.checkbox setFrame:checkboxRect];  
    [self.checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];
    [self.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = self.checkbox;
    array = [NSMutableArray array];
}
return self;
}
-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
if(sender.selected){
    [array addObject:cell]; 
}
}

我在其他課程中使用了該數組,但是它一直給我空數組;

這是我的另一堂課,它給我的日志為零。

-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Implmeneted Yet" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(@"%d",[[names array]count]);

}

![1] http://min.us/m2GMsoRap

我需要一種在單擊按鈕時存儲數據並將其傳輸到其他班級的方法。

EDIT ##完整代碼

#import "AddressBookCell.h"

@implementation AddressBookCell
@synthesize checkbox;
@synthesize array;
@synthesize addressController;



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect checkboxRect = CGRectMake(135, 150, 36, 36);
    [self.checkbox setFrame:checkboxRect];  
    [self.checkbox setImage:[UIImage imageNamed:@"unselected@2x.png"]forState:UIControlStateNormal];
    [self.checkbox setImage:[UIImage imageNamed:@"selected@2x.png"] forState:UIControlStateSelected];
    [self.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.accessoryView = self.checkbox;

    array = [[NSMutableArray alloc]init];

}
return self;

}

-(void)checkboxClicked:(UIButton *)sender{
sender.selected = !sender.selected;
UITableViewCell *cell = (AddressBookCell *)sender.superview;
NSLog(@"%d'",cell.tag);

if(sender.selected){
    [array addObject:cell];  
}else{
    if([array containsObject:cell]){
        [array removeObject:cell];
    }
    NSLog(@"%d", [array count]);
}

}

現在我的另一堂課

-(void)setUpContacts{

NSDictionary *alphabet = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:0],@"A",[NSNumber numberWithInt:1],@"B",[NSNumber numberWithInt:2],@"C",[NSNumber numberWithInt:3],@"D",[NSNumber numberWithInt:4],@"E",[NSNumber numberWithInt:5],@"F",[NSNumber numberWithInt:6],@"G",[NSNumber numberWithInt:7],@"H",[NSNumber numberWithInt:8],@"I",[NSNumber numberWithInt:9],@"J",[NSNumber numberWithInt:10],@"K",[NSNumber numberWithInt:11],@"L",[NSNumber numberWithInt:12],@"M",[NSNumber numberWithInt:13],@"N",[NSNumber numberWithInt:14],@"O",[NSNumber numberWithInt:15],@"P",[NSNumber numberWithInt:16],@"Q",[NSNumber numberWithInt:17],@"R",[NSNumber numberWithInt:18],@"S",[NSNumber numberWithInt:19],@"T",[NSNumber numberWithInt:20],@"U",[NSNumber numberWithInt:21],@"V",[NSNumber numberWithInt:22],@"W",[NSNumber numberWithInt:23],@"X",[NSNumber numberWithInt:24],@"Y",[NSNumber numberWithInt:25],@"Z", nil];


NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for(int i = 0; i<=27; i++){
    [tempArray addObject:[NSNull null]];
}
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
    firstLetter = [info objectForKey:@"lastName"];
    int index = 27;
    if([firstLetter length] > 0){
        firstLetter =[NSString stringWithFormat:@"%C",[firstLetter characterAtIndex:0]];

        firstLetter= [firstLetter capitalizedString];

        if([alphabet objectForKey:firstLetter]){

            NSNumber *t = [alphabet valueForKey:firstLetter];
            index = [t intValue] + 1;
        }
    }
    if([tempArray objectAtIndex:index] == [NSNull null]){
        [tempArray insertObject:[NSMutableArray array] atIndex:index];
    }
    [[tempArray objectAtIndex:index] addObject:info];
}
[alphabet release];    
NSArray *alphabet2 = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
NSMutableArray *tempArray2 = [[NSMutableArray alloc]init];
NSMutableArray *titleTemp = [[NSMutableArray alloc]init];
int c = 0;
for(int i = 0; i<=27; i++){
    if([tempArray objectAtIndex:i] != [NSNull null]){
        if(i == 0){
            [titleTemp insertObject:@"Suggested" atIndex:c];
        }else if(i == 27){
            [titleTemp insertObject:@"Others" atIndex:c];
        }else{
            int loc = i -1;

            [titleTemp insertObject:[alphabet2 objectAtIndex:loc] atIndex:c];
        }
        [tempArray2 insertObject:[tempArray objectAtIndex:i] atIndex:c];
        c++;
    }
}
[alphabet2 release];
[tempArray release];
letterArray = tempArray2;
titlePointer = titleTemp;

}

-(void)textMessage{
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Not Implmeneted Yet" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];

[alert show];
[alert release];*/
AddressBookCell *names = [[AddressBookCell alloc]init];
savedArray = [NSArray arrayWithArray:[names array]];
NSLog(@"%@",savedArray);

}

- (void)viewDidLoad{

[super viewDidLoad];
[self setUpContacts];

indexPaths = [[NSMutableDictionary alloc]init];   

//suggestedPeople = [[NSArray alloc]initWithObjects:@"User1",@"User2",@"User3",@"User4", nil];

//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds        style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = @"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Text"style:UIBarButtonItemStylePlain target:self action:@selector(textMessage)];

//testing section

}

當然它是空的,您可以在另一個類中分配/初始化一個新的AdressBookCell實例,因此它的數組中沒有任何內容。 在另一個類中需要一個屬性,該屬性指向填充數組的第一個類的實例,然后可以使用[pointerToFirstClass array]來獲取該數組。

看來您正在使用手動內存管理。 如果不是,則需要指定。

在這種情況下,使用行[NSMutableArray array]創建[NSMutableArray array] ,將創建一個自動釋放的對象,該對象將在其他類嘗試訪問它之前被釋放。

您需要在創建它時保留它,然后在您的dealloc中釋放它。 如果將[NSMutableArray array]更改為[[NSMutableArray array] retain] ,並向您的dealloc添加[array release]行,則在釋放擁有的對象之前,該數組不會消失。

您還沒有提供足夠的信息來告訴發生了什么事,但是我可以猜測...

日志為零可能意味着一個空數組,或者更可能是一個nil數組...嘗試以下操作:

AddressBookCell *names = [[AddressBookCell alloc]init];
NSLog(@"%@",[names array]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM