Хитрость не в том, чтобы полагаться на ваш cellForRowAtIndexPath
, поскольку он работает только для ячеек, которые видны на экране. Я бы посоветовал выяснить вещи в вашем numberOfSectionsInTableView
. Вот код, который должен работать. Сначала давайте добавим два пути индекса к вашему .h
файлу:
@interface MyTable : UITableViewController {
...
NSIndexPath *ip_top;
NSIndexPath *ip_bottom;
}
@property (nonatomic, retain) NSIndexPath *ip_top;
@property (nonatomic, retain) NSIndexPath *ip_bottom;
А в вашем .m
файле:
...
@implementation MyTable
@synthesize ip_top;
@synthesize ip_bottom;
и, конечно, нам нужно выпустить их в dealloc
:
- (void)dealloc {
[ip_top release];
[ip_bottom release];
}
Теперь давайте возьмем мясо и кости логики. Сначала мы изменим numberOfSectionsInTableView
, так как он запускается один раз при каждой загрузке / перезагрузке таблицы:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv {
NSLog(@"Getting number of sections");
self.ip_top = self.ip_bottom = nil;
int i_numSections = 3;
int i_numRows;
for (int i=0; i<i_numSections; i++) {
i_numRows = [self tableView:tv numberOfRowsInSection:i];
NSLog(@"num_rows:%d for section:%d",i_numRows,i);
if (i_numRows > 0) {
if (!ip_top) {
self.ip_top = [NSIndexPath indexPathForRow:0 inSection:i];
}
self.ip_bottom = [NSIndexPath indexPathForRow:i_numRows-1 inSection:i];
}
}
NSLog(@"top:%@ bottom:%@",ip_top,ip_bottom);
return i_numSections;
}
Обратите внимание, что он найдет первый раздел, содержащий более 1 строки, и укажите ip_top
, в противном случае ip_top
будет nil
. Аналогичная логика для ip_bottom
, которая будет указывать на последний ряд. Теперь нам нужно проверить это в нашем cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if ([indexPath compare:ip_top] == NSOrderedSame) {
NSLog(@"top cell section:%d row:%d",indexPath.section,indexPath.row);
}
if ([indexPath compare:ip_bottom] == NSOrderedSame) {
NSLog(@"bottom cell section:%d row:%d",indexPath.section,indexPath.row);
}
...
}
Имеет смысл? Иди, возьми их!