gpt4 book ai didi

ios - 单选 UITableView 叠加单元格

转载 作者:行者123 更新时间:2023-11-28 21:52:13 29 4
gpt4 key购买 nike

我正在尝试对 UITableView 单元格进行自定义选择。为此,我在我的单元格上方创建了 UIView,它现在在触摸时出现/消失。但问题是当我按下单元格选择时出现。如果那时我选择任何其他行,它也会被选中!但它不能。必须取消选择之前的每个单元格,但我只为我的 UITableView 选择了一个。我很乐意提供任何帮助。

这是我的代码:

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

// Configure the cell...
if (cell == nil) {
cell = [[AVMThemeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AVMDataStore *oneItem = [userContentArray objectAtIndex:indexPath.row];
oneItem = [userContentArray objectAtIndex:indexPath.row];

[cell setGenre:oneItem];
cell.imgView.image = [UIImage imageNamed:imgThemesArray[indexPath.row]];



//save cell state!

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
if ([selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]] )
{
NSLog(@"selectedCellsArray %@",selectedCellsArray);
cell.selectedBG.hidden=NO;
[cell setSelected:NO animated:YES];
}
else
{

cell.selectedBG.hidden=YES;

}


return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
@try{
// TODO: Select Item
if (shareEnabled) {

[selectedCellsArray removeAllObjects];
AVMThemeCell *collectionCell = (AVMThemeCell*)[tableView cellForRowAtIndexPath:indexPath];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];

if ( ![selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]] )
{
[selectedCellsArray addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];

collectionCell.selectedBG.hidden = NO;
[collectionCell setSelected:NO animated:YES];
// NSLog(@"view is %@",collectionCell.selectedBG);
NSLog(@"selected view is hidden = %hhd",collectionCell.hidden);
NSLog(@"selected in didselect %d",(int)indexPath.row);
}
else {
[selectedCellsArray removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
collectionCell.selectedBG.hidden = YES;
NSLog(@"DEselected in didDEselect");
}

}
} @catch (NSException *e){
NSLog(@"Exception! %@",e);
}



}

最佳答案

一个更简单的方法是使用 NSIndexPath

创建 NSIndexPath 变量来跟踪最后选择的单元格。

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

viewDidLoad() 方法中的初始化变量:

self.selectedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];

观察上一行中行的值 -1 和部分的 -1,这将在 tableView 中没有行选择的情况下初始化 indexPath。

现在,UITableView 数据源方法将如下所示:

cellForRowAtIndexPath 方法中放置一个条件来检查当前的 indexPath 是否被选中?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// . . .

// Change background color of selected cell

if (self.selectedIndexPath == indexPath) {
[cell setBackgroundColor:[UIColor orangeColor]];
} else {
[cell setBackgroundColor:[UIColor clearColor]];
}

// . . .
}

didSelectRowAtIndexPath 方法中更新选定的索引路径:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// . . .

AVMThemeCell *previousCell = (AVMThemeCell *)[tableView cellForRowAtIndexPath:self.selectedIndexPath];
previousCell.backgroundColor = [UIColor clearColor];

self.selectedIndexPath = indexPath;
AVMThemeCell *selectedCell = AVMThemeCell *[tableView cellForRowAtIndexPath:self.selectedIndexPath];
selectedCell.backgroundColor = [UIColor orangeColor];

// . . .
}

关于ios - 单选 UITableView 叠加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27915150/

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