Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a UITableViewCell know of its own indexPath?

The standard Grouped UITableView style allows UITableViewCells to be drawn with rounded corners at the top and bottom of each section. How is this accomplished? How does the cell know its own location within its section, and how does it know when to change its rounded edges?

I want to make my own rounded cells, and I have images to use, but don't know when to show which image

Note: I already know how the UITableView works, and I know how to use it. I just thought that since a UITableView is able to automatically draw rounded corners at the correct places, I should be able to as well, without needing to add anything to my data source or delegate.

like image 651
Ed Marty Avatar asked Jul 28 '09 15:07

Ed Marty


People also ask

How does Swift define IndexPath?

In Swift, an indexPath is a list of indexes that, together, represent the path to a specific location in a tree of nested arrays. It describes an item's position inside a table view or collection view, storing both its section and its position inside that section.

How does Swift compare to IndexPath?

The Swift overlay to the Foundation framework provides the IndexPath structure, which bridges to the NSIndexPath class. This means that, as an alternative to NSIndexPath , starting with Swift 3 and Xcode 8, you can use IndexPath . Note that IndexPath also conforms to Equatable protocol. Therefore, you can use == or !=

How do I get the IndexPath of a cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.


3 Answers

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; int rows = [(UITableView *)self.superview numberOfRowsInSection:indexPath.section];  if (indexPath.row == 0 && rows == 1) { // the one and only cell in the section } else if (indexPath.row == 0) { //top } else if (indexPath.row != rows - 1) { //middle } else { //bottom } 
like image 136
Darren Inksetter Avatar answered Oct 02 '22 11:10

Darren Inksetter


It's very simple. suppose cell is the object, whose position is to be found out.

  UITableView* table = (UITableView *)[cell superview];    NSIndexPath* pathOfTheCell = [table indexPathForCell:cell];    NSInteger sectionOfTheCell = [pathOfTheCell section];    NSInteger rowOfTheCell = [pathOfTheCell row]; 
like image 37
J.J. Avatar answered Oct 02 '22 09:10

J.J.


There is sectionLocation method of UITableViewCell that returns integer telling you what you need:

  • 1 - middle cell
  • 2 - top cell
  • 3 - bottom cell
  • 4 - single cell

I had no issues using this in several production apps since 2010.

UPDATE: one of our binaries was automatically rejected recently (end of 2018) because we were using 'sectionLocation' property, so it's not a good option anymore.

Add something like this into your header files and you can use it:

typedef NS_ENUM(NSInteger, MMMTableViewCellLocation) {

    MMMTableViewCellLocationUndefined = 0,
    MMMTableViewCellLocationMiddle = 1,
    MMMTableViewCellLocationTop = 2,
    MMMTableViewCellLocationBottom = 3,
    MMMTableViewCellLocationSingle = 4
};

@interface UITableViewCell ()

/** Undocumented method of UITableViewCell which allows to know where within section the cell is located,
 * so the cell can draw its borders properly. */
- (MMMTableViewCellLocation)sectionLocation;

/** Override this one to know when the value of sectionLocation changes. */
- (void)setSectionLocation:(MMMTableViewCellLocation)sectionLocation animated:(BOOL)animated;

@end
like image 42
aleh Avatar answered Oct 02 '22 11:10

aleh