2013-10-23 112 views
0

我对UITableView使用自定义单元格,并且上面有一个UIButton。我想在触摸时切换按钮的标题。不能更新自定义单元格上的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]; 
     } 
} 

它工作正常进行所有的定制单元,除了一个条件每当我在按钮上点击第一个单元格它会崩溃并显示错误

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

任何帮助将b Ë赞赏...

+0

我想你没有得到标签值为0的UIButton。因此,设置switchButton.tag = indexPath.row + 1,然后相应地检索UIButton。 – Bhavesh

+0

@ iBhavesh:我得到的标签值为0的UIButton。在方法 - (void)addingToPortfolio:(id)发件人它打印按钮标记= 0前崩溃 – Atul

回答

1

这是因为默认情况下的UIView的实例有标签等于0,因此在线路

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

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

+0

感谢您的帮助 – Atul

4

请勿使用标签。将tempButton = (UIButton*)[self.view viewWithTag:self.selectedPath.row];替换为tempButton = (UIButton*)sender;。它应该有所帮助。

相关问题