2013-05-02 33 views
0

我有一個UIButton定製UITableViewCell(其中動態添加單元格),它定義在與MainViewControllerCustomCell類不同的類上。如果選擇了UIButton,我需要MainViewController中的方法返回一個數組。這個數組應該在第n個索引中有關於第n個單元格的信息。 我所寫的是:iOS:從UITableViewCell中創建的按鈕檢索選擇狀態

CustomCell.h

@interface CustomCell : UITableViewCell 
@property (strong, nonatomic) IBOutlet UIButton *tickButton; 

CustomCell.m

- (IBAction)tick:(UIButton *)sender { 

    if ([sender isSelected]) { 
     [sender setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal]; 
     [sender setSelected:NO]; 
    } else { 
     [sender setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected]; 
     [sender setSelected:YES]; 
    } 
} 

MainViewController.m

-(NSMutableArray*) returnTickArray{ 
    NSMutableArray *tickArray = [[NSMutableArray alloc] initWithCapacity:n]; 
    for (NSInteger i=0; i<n; i++){ 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0]; 
    CustomCell *cell = (CustomCell *) [self.itemTable cellForRowAtIndexPath:indexPath]; 
     if ([cell.tickButton isSelected]){ 
      a=[NSNumber numberWithInt:1]; 
     } 
     else { 
      a=[NSNumber numberWithInt:0]; 
     }; 
     [tickArray addObject:a]; 
    } 

    return tickArray; 

} 

我也嘗試過的,而不是使用選擇狀態的按鈕創建一個酒吧lic NSNumber財產並讓UIButton使其在10之間交替,然後在MainViewController上「詢問」,但它也沒有奏效。

回答

1

您正在創建[NSIndexPath indexPathForItem:1 inSection:0];那總是1排。你應該把它替換成'i',就像你在循環中使用的一樣。

+0

非常感謝!我不能相信我錯過了這一點。 – dietbacon 2013-05-02 23:49:11

相關問題