Friday, June 7, 2013

UITableViewCell setAccessoryView example in Objective C (iOS).


UITableViewCell setAccessoryView

A view that is used, typically as a control, on the right side of the cell (normal state).

@property(nonatomic, retain) UIView *accessoryView

Discussion of [UITableViewCell setAccessoryView]
If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) state; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the right side of the cell.[UITableViewCell setAccessoryView]

The accessory view cross-fades between normal and editing states if it set for both states; use the editingAccessoryView property to set the accessory view for the cell during editing mode. If this property is not set for both states, the cell is animated to slide in or out, as necessary.

UITableViewCell setAccessoryView example.
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"textFieldCell"];

UITextField *textFieldView = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
[textFieldView setPlaceholder:@"Placeholder"];

[cell setAccessoryView:textFieldView];

Example of [UITableViewCell setAccessoryView].
switch ( accessoryType )
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
   ...

UITableViewCell setAccessoryView example.
Define a macro for tags of buttons:

#define AccessoryViewTagSinceValue 100000 // (AccessoryViewTagSinceValue * sections + rows) must be LE NSIntegerMax
Create button and set the cell.accessoryView when creating a cell

UIButton *accessoryButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
accessoryButton.frame = CGRectMake(0, 0, 30, 30);
[accessoryButton addTarget:self action:@selector(accessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessoryButton;
Set cell.accessoryView.tag by indexPath in UITableViewDataSource method -tableView:cellForRowAtIndexPath:

cell.accessoryView.tag = indexPath.section * AccessoryViewTagSinceValue + indexPath.row;
Event handler for buttons

- (void) accessoryButtonTapped:(UIButton *)button {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag % AccessoryViewTagSinceValue
                                                inSection:button.tag / AccessoryViewTagSinceValue];

    [self.tableView.delegate tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
Implement the UITableViewDelegate method

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    // do sth.
}

End of UITableViewCell setAccessoryView example article.