`
jsntghf
  • 浏览: 2490110 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

UITableView中cell底端的定位问题

    博客分类:
  • iOS
阅读更多

在UITableView中有一个UILabel,需要处于cell的底端,也就是最后一条cell的下方,但是cell的数量是不定的,也就是说列表可能长,可能短,那么该UILabel应该如何实现定位?

 

可能你最初会想到下面的这种方法。

 

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count] + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SectionsTableIdentifier ];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier: SectionsTableIdentifier ] autorelease];
    }
    if (row == [nameSection count]){
		NSString *str = [[NSString alloc] initWithFormat:@"%@ END", key];
		cell.textLabel.text = str;
	} else {
		cell.textLabel.text = [nameSection objectAtIndex:row];
	}
	return cell;
}

 

其实,有种最简单的方法,只需要实现UITableViewDelegate中的- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;即可。

 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
	NSString *key = [keys objectAtIndex:section];
	NSString *str = [[NSString alloc] initWithFormat:@"%@ END", key];
	UILabel *label = [[[UILabel alloc] init] autorelease];
	[label setText:str];
	[label setTextColor:[UIColor redColor]];
	[label setTextAlignment:UITextAlignmentCenter];
	return label;
}

 

这样,上面的两个方法即可改回原来的实现:

 

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SectionsTableIdentifier ];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier: SectionsTableIdentifier ] autorelease];
    }
	cell.textLabel.text = [nameSection objectAtIndex:row];
	return cell;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics