iOS UITableView单元格选择
我有一个名为NewCell的自定义单元格的UITableView,我试图在单击事件时在NewCell内显示一个名为“selectedView”的UIViewiOS UITableView单元格选择,ios,uitableview,Ios,Uitableview,我有一个名为NewCell的自定义单元格的UITableView,我试图在单击事件时在NewCell内显示一个名为“selectedView”的UIView - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NewCell *cell = (NewCell*)[tableView cellForRowAtIndexPath:indexPath];
- (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;
我觉得这个不错。你能分享一下你的手机代码吗?该死,我知道了。实际上,我仍然为表设置了productCell.selectionStyle。我移除了它,现在它可以工作了。但有了它,它就产生了一个尴尬的副作用,正如我在《凤凰社》中所描述的,很高兴你已经弄明白了。对于未来的人,添加你的解决方案作为答案并接受它。谢谢,但它说我必须等待2天才能这样做,也许你可以。我刚刚加了。