Quickies for UITableView

  • Adding an arrow thingie or checkbox to tableview cells [permalink]
    If you have the cell, you can set the accessoryType directly:
    UITableViewCell *cell = ...; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    The delegate method (accessoryTypeForRowWithIndexPath) has been deprecated, so don't use that.

    Use UITableViewCellAccessoryCheckmark for a checkmark.

    Use UITableViewCellAccessoryNone to remove the checkmark.

  • Adding an index to a tableview [permalink]
    Some tableviews have the short-cut list thingie on the side, which apple calls the index. The index is section-based - each entry in the index corresponds to a section in your data. So if you have a pretty flat list (just an NSArray, for instance), you'll either need to map section-> index, or split up your data so it's physically organized in section/row.

    And then override these. In this case, I split up an array into an array of arrays, each sub-array being the contents of a section that corresponds to a string to display in the index. _tableIndex is an array of strings for display of the index.

    - (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView {     return _tableIndex; } // sectionIndexTitles   - (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) title                 atIndex: (NSInteger) index {     return index; } // sectionForSectionIndexTitle

  • Changing UITableViewCell label text color [permalink]
        if (someProperty) cell.textLabel.textColor = [UIColor grayColor];     else cell.textLabel.textColor = [UIColor blackColor]; 

  • Controlling UITableView row rearranging [permalink]
    Say that you want to let the user rearrange tableview rows except for the first and the last, which can't move. First, inhibit the drawing of the little rearrange indicator on the first and last rows:
    - (BOOL) tableView: (UITableView *) tableView   canMoveRowAtIndexPath: (NSIndexPath *) indexPath {      if (indexPath.row == 0) return NO;     else if (indexPath.row == _cues.count - 1) return NO;     else return YES;  } // canMoveRowAtIndexPath
    And then prevent rows from being dragged to the first and last position:
    - (NSIndexPath *) tableView: (UITableView *) tableView targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source         toProposedIndexPath: (NSIndexPath *) destination {      if (destination.row > 0 && destination.row < _cues.count - 1) {         // No violence necessary.         return destination;     }      NSIndexPath *indexPath = nil;      // If your table can have <= 2 items, you might want to robusticize the index math.     if (destination.row == 0) {         indexPath = [NSIndexPath indexPathForRow: 1  inSection: 0];     } else {         indexPath = [NSIndexPath indexPathForRow: _cues.count - 2                                  inSection: 0];     }      return indexPath;  } // targetIndexPathForMoveFromRowAtIndexPathPleaseOHaiThanksForMovingKthxBai

  • Creating a new index path [permalink]
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row                                           inSection: 0]; 

  • Finding the row from a tableview cell button [permalink]
    You can add buttons to a UITableViewCell. The fun part is figuring out what row that button lives on. Assuming that the button is added right to the cell, you can look at the button's superview to get the cell, and then ask the tableview for the cell's section and row.
    - (IBAction) elaborateType: (id) sender {     if (![sender isKindOfClass: [UIButton class]]) return;  // be paranoid      UITableViewCell *cell = (UITableViewCell *)[sender superview];     if (![cell isKindOfClass: [UITableViewCell class]]) return;  // be paranoid      NSIndexPath *indexPath = [self.kindPickerTableView indexPathForCell: cell];      // do something with indexPath.row and/or indexPath.section.  } // elaborateType

  • Getting selected row from UITableView [permalink]
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];     if (indexPath != nil) [self doStuff]; 

  • Handling UITableView row deletion [permalink]
    - (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle  forRowAtIndexPath: (NSIndexPath *) indexPath {     if (editingStyle == UITableViewCellEditingStyleDelete) {         [_cues removeObjectAtIndex: indexPath.row];  // manipulate your data structure.         [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]                    withRowAnimation: UITableViewRowAnimationFade];         [self updateUI];  // Do whatever other UI updating you need to do.     } } // commitEditingStyle 

  • Invalidating a single row [permalink]
    Sometimes you want to update a single row of a tableview as new information comes in. Such as loading something in the background and you want to update a percentage being shown in the cell. You could -reloadData, but don't. That's an awfully big hammer. Instead just reload a row
           NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row                                               inSection: 0];         NSArray *array = [NSArray arrayWithObject: indexPath];         [_playlistTableView reloadRowsAtIndexPaths: array                             withRowAnimation: UITableViewRowAnimationNone];

  • Laying out tableview cell when entering / exiting edit mode [permalink]
    Entering and exiting UITableView edit mode has a cool animation. If you've got a custom subclass, your goodies don't move unless you override layoutSubviews in your UITableViewCell subclass. Any view position changes will automatically be animated.
    - (void) layoutSubviews {      // skootch stuff over     if (self.editing && !self.showingDeleteConfirmation) { #define MARGIN 40.0         CGRect frame = CGRectMake (MARGIN + 4.0, 4.0, 70.0, 46.0);         _profileView.frame = frame;          frame = CGRectMake (MARGIN + 80.0, 0, 240.0 - MARGIN, 55.0);         _titleLabel.frame = frame;      // layout normally     } else {         CGRect frame = CGRectMake (4.0, 4.0, 70.0, 46.0);         _profileView.frame = frame;          frame = CGRectMake (80.0, 0, 240.0, 55.0);         _titleLabel.frame = frame;     }      [super layoutSubviews]; } // layoutSubviews 
    The showingDeleteConfirmation test is so you don't move things around if the user does the "swipe-right to show delete button" thing.

  • Putting UITableView into editing mode [permalink]
    UITableView editing mode lets you delete / rearrange / insert rows. Turn it on via
        [self.cuesTableView setEditing: YES  animated: YES]; 

  • Rearranging in UITableView [permalink]
    Implementing this method in your UITableView datasource will make the table draw the little rearrange marker on each row.
    - (void) tableView: (UITableView *) tableView moveRowAtIndexPath: (NSIndexPath *) from        toIndexPath: (NSIndexPath *) to {     // How to rearrange stuff if you're backed by an NSMutableArray:     GRCue *cue = [_cues objectAtIndex: from.row];     [cue retain];  // Let it survive being removed from the array.     [_cues removeObjectAtIndex: from.row];     [_cues insertObject: cue  atIndex: to.row];     [cue release];  } // moveRowAtIndexPath 

  • Responding to table taps [permalink]
    Set yourself up as the UITableView delegate first.
    - (void)tableView: (UITableView *) tableView     didSelectRowAtIndexPath: (NSIndexPath *) indexPath {     // Do something logical with indexPath.row, etc. } // didSelectRowAtIndexPath 

  • Scrolling to a row in a UITableView [permalink]
        NSIndexPath *someRow = [NSIndexPath indexPathForRow: random() % blah.count      inSection: 0];     [self.cuesTableView scrollToRowAtIndexPath: someRow          atScrollPosition: UITableViewScrollPositionBottom          animated: YES];
    You can also scroll to PositionTop and PositionMiddle. If you crash, make sure you've done a -reloadData on the table view prior to trying to scroll.

  • Selecting a row in a UITableView [permalink]
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow: index                                           inSection: 0];     [self.tableView reloadData];  // necessary if selecting row in -viewDidLoad      [self.tableView selectRowAtIndexPath: indexPath                     animated: YES                     scrollPosition: UITableViewScrollPositionNone]; 

  • Setting a cell's background color [permalink]
    cell.textLabel.backgroundColor = [UIColor redColor]; cell.contentView.backgroundColor = [UIColor redColor];

  • Setting a cell's image [permalink]
       UIImage *image = [_assets iconAtIndex: indexPath.row];     if (image) cell.imageView.image = image;

  • Table view data source foobage [permalink]
    // Optional - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView {     return 1; } // numberOfSectionsInTableView   - (NSInteger) tableView: (UITableView *) tableView   numberOfRowsInSection: (NSInteger) section {     return dataArray.count; } // numberOfRowsInSection   - (UITableViewCell *) tableView: (UITableView *) tableView           cellForRowAtIndexPath: (NSIndexPath *) indexPath {      UITableViewCell *cell =         [tableView dequeueReusableCellWithIdentifier: @"BlahTableViewCell"];      if (!cell) {         cell = [[UITableViewCell alloc]                    initWithStyle: UITableViewCellStyleDefault                    reuseIdentifier: @"BlahTableViewCell"];         [cell autorelease];     }      cell.textLabel.text = [dataArray objectAtIndex: indexPath.row];      return cell;  } // cellForRowAtIndexPath 

  • Tableview subtitle cell [permalink]
    The subtitle cell has a large label and a smaller gray-text sublabel.

    Make the cell with UITableViewCellStyleSubtitle:

            cell = [[[UITableViewCell alloc]                     initWithStyle: UITableViewCellStyleSubtitle                     reuseIdentifier: @"UITableViewCell"] autorelease]; 
    and set the label values:
        cell.textLabel.text = @"somewhere";     cell.detailTextLabel.text = @"over the rainbow"; 

  • Turning off UITableView scrolling [permalink]
    _tableView.scrollEnabled = NO;
Source : borkware.com

0 komentar:

Posting Komentar