ios - 如何在加载 View 时预先选择 UITableViewCell?

标签 ios iphone objective-c uitableview

我正在研究在表格 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/

相关文章:

iphone - CGContext - 有没有办法重置当前上下文

iphone - Objective-C中的高效循环

iphone - Passkit/存折iOS 5

objective-c - 更改文件大小而不损失质量 iOS

ios - Contre Jour 中的弹性触手等图形效果

ios - "fatal error: unexpectedly found nil while unwrapping an Optional value"尝试将谓词与关系对象一起使用时

ios - 如何注册我的 iOS 应用程序以从 iOS 6+ 的邮件应用程序中打开 zip 文件?

ios - 没有断点就无法播放音频

iphone - iOS : NSString file path to NSURL

ios - 没有模糊 View 效果的 UIAlertController 操作表