iOS: retrieving selection state from button created in UITableViewCell

I have a UIButton on a custom UITableViewCell (to which cells are added dynamically), which is defined on a different class than the MainViewController, the CustomCell class. I need a method in the MainViewController to return an array with if the UIButtons are selected or not. This array should have in the n-th index the information about the n-th cell. What I have written is:

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;

}

I have also tried instead of using the button selected state to create a public NSNumber property and having the UIButton make it alternate between 1 and 0, and then "asking for it" on MainViewController, but it also hasn't worked.

Answers


You are creating [NSIndexPath indexPathForItem:1 inSection:0]; that is always 1 row. you should replace it with 'i' as you are using in the loop.


Need Your Help

iPad: Content view is not resized to fit popover

ios uiviewcontroller resize storyboard uipopovercontroller

I'm having some trouble with presenting a content view in a popover. I'm using the following code to set up the popover with a view controller which I instantiate from the storyboard. The view cont...

What are the conditional jump instructions for Go's assembler?

assembly go

Go's 6a assembler has conditional jump instructions:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.