我正在研究在桌面视图中选择单元格时播放声音的内容。 tableView由带有声音名称的NSStrings数组填充。
我有一个对象作为具有声音对象的控制器的一部分,当您单击该单元格时,它会播放声音对象的正确文件名称。
如何在视图首次加载时预先选择一个TableView单元格,并使其在tableView首次加载时不播放声音。
这是我现在的选择代码。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TMVSoundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SoundCell" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
- (void)configureCell:(TMVSoundCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.soundTitle.text = [[self.soundArray objectAtIndex:indexPath.row]name];
if (self.selectedRowIndexPath != nil && [indexPath compare:self.selectedRowIndexPath] == NSOrderedSame)
{
cell.soundTitle.textColor = [UIColor whiteColor];
self.itemView.item.sound = [self.soundArray objectAtIndex:indexPath.row];
[DataManager saveContext];
}
else
{
cell.soundTitle.textColor = AppDelegate.atmosphere.currentColor;
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];
if (self.selectedRowIndexPath)
{
if ([indexPath compare:self.selectedRowIndexPath] == NSOrderedSame)
{
self.selectedRowIndexPath = nil;
}
else
{
[indexPaths addObject:self.selectedRowIndexPath];
self.selectedRowIndexPath= indexPath;
}
}
else
{
self.selectedRowIndexPath = indexPath;
}
[tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
答案 0 :(得分:3)
使用selectRowAtIndexPath
以编程方式选择单元格。
根据docs:
调用此方法不会导致委托接收 tableView:willSelectRowAtIndexPath:或 tableView:didSelectRowAtIndexPath:message,也不会发送 向观察者发出UITableViewSelectionDidChangeNotification通知。
答案 1 :(得分:3)
在viewDidAppear
中添加此代码(在viewDidAppear中以确保您的tableView已加载)
UITableViewCell *cellToSelect = [self.tableView cellForRowAtIndexPath:self.selectedRowIndexPath];
[cellToSelect setHighlighted:YES animated:YES];
您的单元格将在不调用委托方法的情况下被选中,因此不会播放声音。
答案 2 :(得分:1)
你可以打电话给
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
viewDidLoad或viewWillAppear中的方法,它将选择行而不调用didSelectRowDelegate
希望有所帮助
丛
答案 3 :(得分:1)
您需要在selectRowAtIndexPath
中致电cellForRowAtIndexPath
。 Swift4 :
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if ADD_YOUR_CONDITION_HERE {
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .top)
}
return cell
}
答案 4 :(得分:0)
在您的cellForRowAtIndexPath方法中,检查单元格是否适用于您要选择的行。如果是,请将其设置为选中状态。 cell.selected = YES
或者,[self.tableView selectRowAtIndexPath:self.selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self.tableView didSelectRowAtIndexPath:self.selectedIndexPath];`
答案 5 :(得分:0)
[self.soundArray enumerateObjectsUsingBlock:^(Sound *obj, NSUInteger idx, BOOL *stop)
{
if ([obj.name isEqualToString:self.itemView.item.sound.name])
{
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
}
}];
我是个白痴。我从没想过把它放在我的conifgureView方法中。
感谢所有帮助过我的人。