Reputation: 585
I'm working on something that plays an sound when a cell is selected in a table view. The tableView is populated by an Array of NSStrings with sound names.
I have an object as part of the controller that has a sound object, and when you click the cell it plays the sound objects correct file for the name.
How can I have a TableView cell pre selected when the view first loads, and have it so it doesn't play the sound when the tableView first loads.
Here's my selection code as of now.
- (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];
}
Upvotes: 2
Views: 4139
Reputation: 6697
You need to call selectRowAtIndexPath
in the cellForRowAtIndexPath
. An example in 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
}
Upvotes: 5
Reputation: 1767
In your cellForRowAtIndexPath method, check to see if the cell is for the row you want to be selected. If it is, set it selected. cell.selected = YES
Alternatively, [self.tableView selectRowAtIndexPath:self.selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self.tableView didSelectRowAtIndexPath:self.selectedIndexPath];`
Upvotes: 0
Reputation: 585
[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]];
}
}];
I'm such an idiot. I never thought of putting this in the my conifgureView method.
Thank you to everyone who helped me out.
Upvotes: 0
Reputation: 4919
In viewDidAppear
add this code (in viewDidAppear to make sure your tableView has loaded)
UITableViewCell *cellToSelect = [self.tableView cellForRowAtIndexPath:self.selectedRowIndexPath];
[cellToSelect setHighlighted:YES animated:YES];
Your cell will be selected without calling the delegate method, so the sound won't be played.
Upvotes: 3
Reputation: 56
You can call the
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
method in your viewDidLoad or viewWillAppear and it will select the row and not call your didSelectRowDelegate
Hope that helps
Cong
Upvotes: 1
Reputation: 53830
Use selectRowAtIndexPath
to select a cell programmatically.
According to the 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.
Upvotes: 7