gpt4 book ai didi

iOS UITableView 单元格选择

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:39 26 4
gpt4 key购买 nike

我有一个带有名为 NewCell 的自定义单元格的 UITableView,我试图在点击事件时在 NewCell 中显示一个名为“selectedView”的 UIView。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NewCell *cell = (NewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.selectedView setHidden:NO];
}

编辑 - 添加单元格创建代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewCell";
NewCell *productCell = (NewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (productCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:nil options:nil];

for (id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
productCell = (NewCell *) currentObject;
break;
}
}
}

Product *mainProduct = [self.productsArray objectAtIndex:indexPath.row];

float floatPrice = [mainProduct.price floatValue];
NSString *priceFormat;
if ((int)floatPrice == floatPrice) priceFormat = @"$%.0f"; else priceFormat = @"$%.2f";

produtCell.productPrice.text = [NSString stringWithFormat:priceFormat, floatPrice];

return productCell;
}

一切正常,表格会填充,但是当我选择一个单元格时,它不会在表格中选择的单元格上显示 selectedView,而是在相反的单元格中显示 selectedView。我的意思是,如果屏幕上显示第 0 行和第 1 行并且我选择第 1 行,它将在第 0 行显示 selectedView,如果我选择第 0 行,它将在第 1 行显示 selectedView。

很奇怪,我似乎无法说出它为什么这样做。

我一定是做错了什么。

最佳答案

这样做的原因是因为您需要将选择样式设置为无,如下所示:

productCell.selectionStyle = UITableViewCellSelectionStyleNone;

关于iOS UITableView 单元格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11435753/

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