2012-03-30 63 views
2

我有一个UITableView有3个部分,每个返回numberOfRows 3个独立数组中的计数。在我的cellForRow中,我需要能够检测哪个单元是表中的第一个单元,哪个是最后一个。确定第一个和最后一个UITableViewCell

棘手的部分是,有时一个部分可以返回0的计数的行数,所以我有一个很难搞清楚了这一点。我想要做的是设置两个标志:isTopCellisBottomCell。有任何想法吗?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section==0) 
     return [array1 count]; 
    else if(section==1) 
     return [array2 count]; 
    else return [array3 count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

if(/*something, what I'm trying to figure out*/) 
     isTopCell=YES; 
if(/*something, what I'm trying to figure out*/) 
     isBottomCell=YES; 
} 

编辑:让我澄清。这三个部分是统一的。我想确定整个顶部和底部的单元格。如果(indexPath.row == 0)将为3个部分返回true。我只需要1个赢家。

回答

2

试试这个:

int index = indexPath.row; 
if (indexPath.section > 0) index += [array1 count]; 
if (indexPath.section > 1) index += [array2 count]; 

if (index == 0) // Top row 

if (index == [array1 count] + [array2 count] + [array3 count] - 1) // Bottom row 
+0

如果0节返回0的计数,该怎么办?这将打破 – Snowman 2012-03-30 13:55:31

+0

@mohabitar谢谢,这应该是现在修复。 – dasblinkenlight 2012-03-30 14:01:30

0

第一个

if(indexPath.row == 0) 

最后

if(indexPath.row == sectionarray.count - 1) 

与sectionarray我的意思当然是你的阵列1-3 你将不得不决定与

indexPath.section 
使用

这将返回值0,1,2为你3节=)

或者你可以得到这样的

[self tableView:tableView numberOfRowsInSection:indexPath.section] 
1

没有什么猫腻吧计数。这是部分任意数量的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSInteger totalIndex = indexPath.row; 
    NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView]; 
    NSInteger totalNumberOfRows = 0; 

    for (NSInteger currentSection = 0; currentSection < numberOfSections; currentSection ++) { 
     NSInteger rowsInSection = [self tableView:tableView numberOfRowsInSection:currentSection]; 
     totalNumberOfRows += rowsInSection; 
     if (currentSection < indexPath.section) totalIndex += rowsInSection; 
    } 

    BOOL isTopCell = (totalIndex == 0); 
    BOOL isBottomCell = (totalIndex == totalNumberOfRows); 
} 
+0

但是,如果该部分是1并且该行是0呢?然后它不是顶层单元格,它是一个中间单元格 – Snowman 2012-03-30 13:52:14

+0

哦,您需要**表格中的顶部和底部行。然后它甚至更少*棘手。 – yuji 2012-03-30 13:56:45

+0

这仍然行不通。 topCell不一定必须位于第0部分。如果第0部分和第1部分都返回0行,它可以是第2部分。 – Snowman 2012-03-30 14:14:33

0

这应该对任何表视图的工作 - 部分的任意数量以及每个部分中的行任意数量。它也处理只有一行的情况(通过将行检测为顶行和底行)。只需将其添加到您的cellForRowAtIndexPath方法的顶部即可。

BOOL isTop = NO; 
BOOL isBottom = NO; 
int sections = [self numberOfSectionsInTableView:tableView]; 
if (indexPath.row == 0) { 
    // Could be first 
    isTop = YES; 
    for (int i = indexPath.section-1; i>=0; i--) { 
    if ([self tableView:tableView numberOfRowsInSection:i] > 0) { 
     // Non-empty section above this one, so not first. 
     isTop = NO; 
     break; 
    } 
    } 
} 
if (indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section]-1) { 
    // Could be last 
    isBottom = YES; 
    for (int i = indexPath.section + 1; i < sections; i++) { 
    if ([self tableView:tableView numberOfRowsInSection:i] > 0) { 
     // Non-empty section below this one, so not last. 
     isBottom = NO; 
     break; 
    } 
    } 
} 
0

我已经在我的项目做的是

我使用followingg代码,我已经设置的1图像和这将是从细胞的其余部分不同的表视图的最后一个单元格

// 
    // Set the background and selected background images for the text. 
    // Since we will round the corners at the top and bottom of sections, we 
    // need to conditionally choose the images based on the row index and the 
    // number of rows in the section. 
    // 
    UIImage *rowBackground; 
    UIImage *selectionBackground; 
    NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]]; 
    NSInteger row = [indexPath row]; 
    if (row == 0 && row == sectionRows - 1) 
    { 
     rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; 
     selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"]; 
    } 
    else if (row == 0) 
    { 
     rowBackground = [UIImage imageNamed:@"topRow.png"]; 
     selectionBackground = [UIImage imageNamed:@"topRowSelected.png"]; 
    } 
    else if (row == sectionRows - 1) 
    { 
     rowBackground = [UIImage imageNamed:@"bottomRow.png"]; 
     selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"]; 
    } 
    else 
    { 
     rowBackground = [UIImage imageNamed:@"middleRow.png"]; 
     selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"]; 
    } 
    ((UIImageView *)cell.backgroundView).image = rowBackground; 
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 

    // 


#endif 

让我知道它的工作 干杯!!!!

0

我假设你只想顶部和底部为整个表,而不是每个部分。首先想到的是创建一个数组,它是array1,array2和array3的连接内容。然后,当您在cellForRowAtIndexPath中执行正常的单元配置时:将使用section和row检索的元素与累积数组的第一个和最后一个元素进行比较。

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    BOOl isTopCell = NO; 
    BOOl isBottomCell = NO; 

    int totalNumberOfCells = [array1 count] + [array2 count] + [array3 count]; 

    int cellOverallPosition = 0; 

    switch (indexPath.section) { 
     case 0: { cellOverallPosition = indexPath.row; } break; 
     case 1: { cellOverallPosition = indexPath.row + [array1 count]; } break; 
     case 2: { cellOverallPosition = indexPath.row + [array1 count] + [array2 count]; } break; 
    } 

    if (cellOverallPosition == 1) { isTopCell = YES; } 

    if (cellOverallPosition == totalNumberOfCells) { isBottomCell = YES; } 

    //Other cell stuff follows 

} 
1

技巧是不依赖于你的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第一部分将是nilip_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); 
    } 

    ... 
} 

是否有意义?去得到他们!

相关问题