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

并在 Function addedToPortfolio

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

它适用于所有自定义单元格,除了一个条件,每当我单击第一个单元格上的按钮时,它就会崩溃并显示错误

-[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 回答
1

这是因为默认情况下 UIView 实例的标记等于 0。因此在行

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

您将获得标签为 0 的众多子视图之一(您的按钮在其中,但显然返回了其他视图,导致崩溃)。将标签设置为 index.row + 1 ,它应该可以正常工作。

于 2013-10-23T13:38:38.097 回答