Question: I created a custom UITableViewCell which should have a different appearance than the default implementation. I need to customize the appearance and/or position of standard iPhone red buttons of edit and delete confirmation that seems to be out of reach. Maybe my colleagues are completely on the wrong track here and maybe subclassing a UItableViewCell is a bad idea, but I have been told to do so. How?
Answer: Not the worst track. Look at this code for some hints for animating or changing the appearance of Delete Confirmation or Edit Control buttons:
When you subclass UITableViewCell, and override the layoutSubviews method, you can even draw inside the contentView, or insert subviews on the fly. layoutSubviews gets called when the cell gets setNeedsLayout.
On answering a question herewith we show you another example:
The above code is following the changes in the edit state to show the instruction text appropriately.
Interface Builder is a great tool for building complex views. To my experience, in most cases there’s really no need to subclass UITableViewCell. You can design and implement your own cells, including your own custom Edit and Delete buttons.
Notes:
1. Do not add your custom content to the cell’s backgroundView. Use it for decoration only. You must put your own controls in the cell’s contentView. The contentView is properly resized by Edit button, on the left, or the Delete Confirmation button, on the right.
2. Yes. You can change the title of the Delete Confirmation button without subclassing. Implement: (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath. For details google or bing for ‘UITableView class reference’.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellForTest";
int row = indexPath.row;
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil ){
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// .......
}
// Interface, Implementation:
@interface MyTableViewCell : UITableViewCell {
}
@end
@implementation MyTableViewCell
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
{
// Do whatever you want with this Delete Confirmation subview
}
// use the edit state control mask and... if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]){
// Do whatever you want with this Edit Control subview
// }
}
}
[super willTransitionToState:state];
}
- (void) didTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
{
// Do whatever you want with this subview
}
}
}
}
- (void) layoutSubviews {
[super layoutSubviews];
// you can manipulate cell subviews, like self.textLabel, self.detailTextLabel...
}
@end