我尝试自定义选择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);
}
}
答案 0 :(得分:2)
更简单的方法是使用NSIndexPath
。
创建NSIndexPath
变量以跟踪上次选择的单元格。
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
viewDidLoad()
方法中的初始化变量:
self.selectedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
观察行的值-1
和上面一行中-1
的部分,它将初始化indexPath而不在tableView中选择行。
现在,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];
// . . .
}