0

UITableView にカスタム セルを使用しており、その上に UIButton があります。ボタンがタッチされるたびに、ボタンのタイトルを切り替えたい。

私がやっているcellForRowAtIndexPathで

- (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;
}

そして関数の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];
        }
}

クラッシュしてエラーが表示される最初のセルのボタンをクリックするたびに、1 つの条件を除いて、すべてのカスタム セルで正常に動作しています。

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

どんな助けでも大歓迎です...

4

2 に答える 2

4

タグは使用しないでください。に置き換えtempButton = (UIButton*)[self.view viewWithTag:self.selectedPath.row];ますtempButton = (UIButton*)sender;。それは役立つはずです。

于 2013-10-23T13:40:21.920 に答える