UITableViewCell. accessoryType problem

I have a custom table view cell which inherits from UITableViewCell. In its content view I have added a UISegmentedControl.

When I change the selection in the UISegmentedControl I modify the table view cell's accesstoryType.

Although this works (the appearance of the table view cell in the table changed to use the new accessory type), I have discovered that the table view it belongs appears to stop firing the tableView:accessoryButtonTappedForRowWithIndexPath: on the table view's UITableViewDelegate delegate.

My table view cell toggles the accessoryType between UITableViewCellAccessoryNone and UITableViewCellAccessoryDetailDisclosureButton.

Has anyone else noticed this? Am I going about this the wrong way?

Thanks for any help, Dave.

Posted on Jun 3, 2009 1:22 PM

Reply
9 replies

Jun 5, 2009 11:12 AM in response to K T

Something like this:

- (void) changedModeAction:(id)sender
{
// User changed the alarm mode.
switch (modeSegmentedControl.selectedSegmentIndex)
{
case 0:

self.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
break;

default:

self.accessoryType=UITableViewCellAccessoryDetailNone;

break;
}
}


The accessory appears and disappears as expected but clicking on it does not invoked the table view controller's


- (void) tableView:(UITableView *)aTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath


Commenting out the accessoryType lines in the above method makes it work again.

Thanks, Dave,

Jun 6, 2009 5:27 PM in response to Dave iMac

Oops, sorry I missed that - most likely this is your problem though: you are changing the cell and the tableView doesn't know anything about it (so it is not aware that there now is an accessoryButton on a cell that previously didn't have one). You should try to call the reloadData on the tableView which contains those cells after changing the cell itself (I believe the tableView should be accessible via the superView property hierarchy…)

Andreas User uploaded file

Jun 6, 2009 6:34 PM in response to Dave iMac

There are several ways you can find the address of the table view object from inside a cell method (e.g. see [http://discussions.apple.com/thread.jspa?messageID=9504961&#9504961]). But to test Andreas' suggestion, I would just add an ivar/property to your cell subclass. For example if the table view data source is a controller subclass named MyTableController:

@interface MyCellType : UITableViewCell {
// ,,,
UITableView *tableView;
}
@property (nonatomic, assign) UITableView *tableView;
@implementation MyTableController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ,,,
if (cell == nil) {
// ...
cell.tableView = tableView

I have some other questions and suggestions for you as well (though I would definitely try Andreas' idea first)::

1) Just to check if I understand your original post, when the code in changedModeAction isn't commented out, are you getting taps in the delegate up until the time you first run changedModeAction?

2) Which accessory type is the default? I.e. before you first run changedModeAction: which type is there? If the default isn't UITableViewCellAccessoryDetailDisclosureButton, try making that the default (i.e. set that type in cellForRowAtIndexPath);

3) In the method you posted (repeated below), who is sender and who is self?

- (void) changedModeAction:(id)sender {
// User changed the alarm mode.
switch (modeSegmentedControl.selectedSegmentIndex)
{
case 0:
self.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
break;
default:
self.accessoryType=UITableViewCellAccessoryDetailNone;
break;
}
}

4) Are you tracking the type change in cellForRowAtIndexPath? Put a NSLog() in cellForRow.. and see if it runs after the switch.

Jun 7, 2009 5:05 AM in response to RayNewbie

1 - If I comment out the code in the method the delegate fires when I tap the accessory. If I put the code back in the delegate still fires the accessory tap method tableView:accessoryButtonTappedForRowWithIndexPath. However, when I tap on the segmented control and the code is fire then the accessory no longer fires the method tableView:accessoryButtonTappedForRowWithIndexPath. When I changed the segmented control's value then I can see the accessory change in the UI. I can also see the accessory being tapped. It is almost as if something is getting disconnected when I change the value of accessoryType?

2 - I'll try this - thanks for the suggestion.

3- The sender is the segmented control and self is the instance of the table view cell.

4- I'll try this too - thanks.

Thanks, Dave.

Jun 7, 2009 4:57 PM in response to Dave iMac

Dave iMac wrote:
It is almost as if something is getting disconnected when I change the value of accessoryType?

That's my thought too. I was looking at this some more, and I think the correct way to change the type is to have some code that tracks the change in tableView:accessoryTypeForRowWithIndexPath:. Not sure when that method is called, but if it's called for every visible cell you wouldn't need to reloadData. I'm thinking there's some internal step that isn't happening because the table wants to get the change order from the delegate method. To test this, I would remove any code that tracks the change in cellForRowAtIndexPath (my idea no. 4).

Not sure how to implement the above though. I might try adding another ivar to the cell and have accessoryTypeForRow.. change the type based on that ivar. Possibly that method only needs to return the type that's already set ...doesn't make much sense, but that never stopped me from trying something in Cocoa. Anyway, if we assume the delegate method is there for a reason, maybe we just need to figure out how to use it.

\- Ray

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UITableViewCell. accessoryType problem

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.