gpt4 book ai didi

ios - 无法更新自定义单元格上的 UIButton 标题

转载 作者:行者123 更新时间:2023-11-28 18:35:46 25 4
gpt4 key购买 nike

我正在为 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];
}
}

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

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

任何帮助将不胜感激...

最佳答案

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

关于ios - 无法更新自定义单元格上的 UIButton 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19542947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com