问题
I have a UITableViewCell
that on touch, expands (dropdown). The user is then presented with a selection of options. When the user touches one of the options I want to briefly hide all the options, show an activity indicator spinning, display a confirmation message (on success) and then collapse the cell back to normal.
I'm having trouble hiding any object within the custom cell. The following simple code doesn't work (this is the correct superview corresponding to the cell):
var customCell: MyCustomCell = icon.superview.superview.superview as MyCustomCell
customCell.myLabel.hidden = false
I have tried hiding/showing a variety of different objects but nothing works. I've even popped it inside dispatch_async
to ensure it runs on the main thread.
Additionally, Xcode 6 beta tells me that myLabel.hidden
is read only. This also happens for other objects. Is this no longer the correct way to hide something?
I've had a search around but had no luck in finding an answer. If someone could help me out I'd be grateful! Thanks.
回答1:
The usual way of doing something like this would be to implement the UITableViewDelegate method -tableView:didSelectRowAtIndexPath:
to let you know when a row is tapped.
Then you can easily get and modify the cell
var customCell = self.tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
customCell.myLabel.hidden = false
I consider the line icon.superview.superview.superview
to be a code smell. The multiple superview calls makes makes your code fragile and likely to break if you ever change the view hierarchy in your cell.
来源:https://stackoverflow.com/questions/24663294/hiding-objects-within-uitableviewcell-ios-8-swift