在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;
}
分享到:
相关推荐
swift现实UITableView的cell自定义以及删除与添加,添加表的刷新功能
UITableView 删除 Cell Demo Delete
当UITableView遇到多cell时,往往我们要建立多个cell,这是就会显得类多,臃肿。 demo采用了工厂模式,用父类cell指针指向子类cell,减少类臃肿,不好维护的效果
UITableView在两个Cell中间显示目录效果,点击列表某一项,在该项下面显示菜单,不挡住下一项,很实用的例子。
UITableView的cell移动位置,下次进入界面后是拖动后的界面
2.实现了可删除的tableViewCell,向左划动某个cell,cell右侧会出现“删除”按钮,点击即可将当前cell删除,删除伴有简单的动画效果。 3.实现了tableView的下拉刷新功能,下拉tableView的过程中,将依次显示“下拉...
ios 根据文件内容自动设置UITableView(cell)行高 在程序开发中,很多数据是动态获取的,通常UITableView中的行高设置为固定的,当文字量比较多时,程序会自动隐藏多余的数据,并且在可以显示的位置最后加‘…’,...
长按即可移动cell的UITableView
使用的是MVC编程框架,KVO数据模型,仅供参考
UItableview cell (自定义)增假,删除 移动
在程序开发中,很多数据是动态获取的,通常UITableView中的行高设置为固定的,当文字量比较多时,程序会自动隐藏多余的数据,本文讲述自定义设置行高
使用场景:UIViewController 上添加添加UITableView,UITableView的cell 时限向左 向右滑动出现多个操作按钮,可以根据具体使用场景修改代码
利用纯代码实现,两种最容易实现自适应高度的方法。一看就能学会。
UITableViewDropDown 点击cell展开另外一个UITableView
给 UITableview 添加阴影和变现,并且阴影渐变等样式的设计
iOS+折叠文本+UILabel文本折叠+UITableView内UITableViewCell文本折叠
实现了cell交换的简单动画,代码分析和bug放在地址,上面只有VC,和cell的核心代码,需要导入Mansory库
*****简单使用XIB文件创建自定义的cell*
*****通过storyboard创建自定义cell并使用Model数据模型**