我正在研究在表格 View 中选择单元格时播放声音的东西。 tableView 由带有声音名称的 NSString 数组填充。
我有一个对象作为 Controller 的一部分,它有一个声音对象,当您单击单元格时,它会播放名称正确的声音对象文件。
如何在首次加载 View 时预先选择 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];
}
最佳答案
使用 selectRowAtIndexPath
以编程方式选择单元格。
根据docs :
Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.
关于ios - 如何在加载 View 时预先选择 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21958389/