質問

I am using Custom Cell for UITableView and there is a UIButton on it. I want to toggle the title of the Button whenever is touched.

in cellForRowAtIndexPath I am doing

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    NSLog(@"Creating Cell");

    FADynamicCell *cell= (FADynamicCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([FADynamicCell class])
                                             owner:nil
                                           options:nil] lastObject];
    }


    switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    switchButton.tag = indexPath.row;
//    switchButton.titleLabel.tag = indexPath.row;
    NSLog(@"Swithch Button Tag = %ld",(long)switchButton.tag );
    switchButton.frame =  cell.addToPFButton.frame;
    switchButton.backgroundColor = [UIColor colorWithRed:102./255 green:204./255 blue: 255./255 alpha:1];
    switchButton.titleLabel.numberOfLines = 2;
    switchButton.titleLabel.font = [UIFont systemFontOfSize:12];
    switchButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [switchButton setTitle:@"Remove From Portfolio" forState:UIControlStateNormal];
    [switchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [switchButton addTarget:self action:@selector(addingToPortfolio:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:switchButton];

    return cell;
}

And in Function addingToPortfolio

-(void) addingToPortfolio:(id) sender
{
    NSLog(@"In FADymanicCellTable");
    foundInPortfolio = [TOperations foundInPortfolio:selectedSymbol];
    tempButton = (UIButton*)[self.view viewWithTag:self.selectedPath.row];
    NSLog(@"Self selected path row = %ld", (long)self.selectedPath.row );
    NSLog(@"Button tag = %d", tempButton.tag);
if (foundInPortfolio)
        {
            NSLog(@"Removing From Portfolio");
            [TOperations deleteFromPortfolio:selectedSymbol];
            tempButton.titleLabel.font = [UIFont systemFontOfSize:12];
            [tempButton setTitle:@"Add To Portfolio" forState:UIControlStateNormal];
        }
        else
        {
            NSLog(@"Adding to Portfolio");
            [TOperations addToPortfolio:selectedSymbol];
            tempButton.titleLabel.font = [UIFont systemFontOfSize:10];
            [tempButton setTitle:@"Remove From Portfolio" forState:UIControlStateNormal];
        }
}

It is working fine for all the Custom Cells except for one condition whenever I click on the button on the first cell it gets crashed and shows the error

-[UIView titleLabel]: unrecognized selector sent to instance 0x86e4850
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView titleLabel]: unrecognized selector sent to instance 0x86e4850'

Any Help will be appreciated...

役に立ちましたか?

解決 2

It is because by default an instance of UIView has tag equal to 0. Therefore in line

tempButton = (UIButton*)[self.view viewWithTag:self.selectedPath.row];

you get one of the many subviews with tag 0 (your button is among them, but apparently some other view is returned, causing a crash). Set tags as index.row + 1 instead and it should work fine.

他のヒント

Do not use tags for it. Replace tempButton = (UIButton*)[self.view viewWithTag:self.selectedPath.row]; with tempButton = (UIButton*)sender;. It should help.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top