使用空白view取代cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中颜色
UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView = backView;
cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
//取消边框线
[cell setBackgroundView:[[UIView alloc] init]]; //取消边框线
cell.backgroundColor = [UIColor clearColor];
}
//在navigation中tableviewCell选中后返回无选中项
//单击一个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if(cell.tag == 0){
//注销cell单击事件
cell.selected = NO;
}else {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]; //取消选中项
BabyScheduler *babyScheduler=[listData objectAtIndex:indexPath.row-1];
[delegate showVaccinationView:babyScheduler];
}
}
- (void)viewDidLoad
{
self.title = NSLocalizedString(@"TempGroupViewTitle", @"");
self.view.backgroundColor=[UIUtils defaultViewBackground];
self.tempGroupTableView.backgroundColor=[UIColor clearColor];
self.tempGroupTableView.separatorColor=[UIColor clearColor]; //分割cell线颜色
self.tempGroupTableView.separatorStyle=UITableViewCellSeparatorStyleNone; //不带分割线样式
self.tempGroupTableView.rowHeight=45.0;
self.navigationItem.rightBarButtonItem = self.editButtonItem; //添加navigation按钮
self.groupList = [DBManager selectTempGroup]; //获取分组信息
// NSLog(@"-----%d",[groupList count]);
[super viewDidLoad];
}
if (!cell)----当cell为空?真:假
//设置cell的高度
#pragma mark - Table view delegate
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section==1)return 45;
return 0;
}
//返回自定义hrader
-(UIView*) tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
if (section==1) { //第二区
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 38)];
UIImageView* backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title.png"]];
backgroundView.frame=CGRectMake(0, 0, 123, 38);
[view addSubview:backgroundView];
[backgroundView release];
view.backgroundColor=[UIColor clearColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(30, 0, 93, 38)];
label.backgroundColor=[UIColor clearColor];
label.textColor=[UIColor whiteColor];
label.text=NSLocalizedString(@"Section_Title_My_Group_Name", @"");
[view addSubview:label];
[label autorelease];
return [view autorelease];
}
return nil;
}
//向tableview填充数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//当第一个分区得最后一行
if ((indexPath.section==0)&&(indexPath.row==[groupList count])) {
static NSString *AddGroupViewCellIdentifier = @"AddGroupViewCell";
UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddGroupViewCellIdentifier] autorelease];
// key 说明性文字
cell.textLabel.text=NSLocalizedString(@"Add_New_Group", @"add new group");
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.textAlignment=UITextAlignmentCenter; //cell中text文本居中
cell.backgroundColor=[UIUtils defaultContactCellBackgroundColor];
cell.tag=-1;
return cell;
}
static NSString *SimpleTableIdentifier = @"GroupListViewCell";
//使用自定义cell
//查找SimpleTableIdentifier的cell,为空初始化
GroupListViewCell *cell = (GroupListViewCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (!cell)
{
[[NSBundle mainBundle] loadNibNamed:SimpleTableIdentifier owner:self options:nil];
cell = groupCell;
cell.backgroundColor=[UIUtils defaultContactCellBackgroundColor];
self.groupCell = nil;
}
cell.group=[groupList objectAtIndex:indexPath.row];
//设置cell右边箭头,v等等,有枚举变量可供选择
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSUInteger row = [indexPath row];
cell.tag = row;
[SimpleTableIdentifier release];
return cell;
}
cell可删除
// 指定tableview可删除的区域
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.section==1?YES:NO;
}
//可删除的cell
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
if (row == [groups count]) {
return UITableViewCellEditingStyleNone;
}else {
return UITableViewCellEditingStyleDelete;
}
}
// 删除之后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self deleteGroup:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
//选中cell时取消选中的颜色一直显示
注意这个要在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中写!!
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//当前选中行设为非选中
[self.membersListView deselectRowAtIndexPath:membersListView.indexPathForSelectedRow animated:YES];
分享到:
相关推荐
在iOS开发中,UITableView是一种常用的UI组件,常用于展示列表数据。"UITableView单选"指的是在UITableView中实现单选功能,即用户只能选择一个条目,而不能同时选择多个。这个功能在许多应用场景中都很常见,例如在...
这篇博客“用UITableView 进行多选的代码例子”显然是探讨如何在UITableView中实现多选功能,这是在开发中处理批量操作或需要用户进行选择时常用的一个特性。我们将深入探讨这个主题,了解如何在实际项目中实现这一...
在iOS开发中,UITableView是一种常用的UI组件,用于展示列表数据。然而,系统默认的多选模式下,每个单元格(cell)左侧的复选框是固定的,无法直接自定义。标题"ios-自定义tableView多选状态下左边的图片.zip"所...
3. **处理选择**:在`UITableViewDelegate`的`tableView(_:didSelectRowAt:)`方法中,我们需要处理行的选中和取消选中。为了实现全选/全取消功能,我们需要遍历所有可见行,并调用`tableView.selectRow(at: animated...
在iOS开发中,UITableView是一种常用的UI组件,用于展示列表数据,比如在地址选择或购物车功能中。这个“ios-tableview多选与单选,用于地址,购物车.zip”文件包含了一个实现UITableView单选和多选功能的示例。下面...
在iOS开发中,UITableView是一种常用的数据展示控件,它用于显示一列或多列可滚动的数据。本Demo专注于实现TableView的单选功能,这对于创建如设置菜单、选项列表等场景非常实用。下面我们将深入探讨实现这一功能的...
在iOS开发中,UITableView是展示数据列表常用的控件,它提供了单选和多选功能。在实际应用中,比如在设置界面或者通讯录选择中,我们经常需要实现多选功能。下面将详细介绍如何在iOS中实现`tableView`的多选功能。 ...
在iOS开发中,PickerView(选择器视图)是一种常用组件,用于展示一系列选项供用户选择,例如日期、时间或一组预设值。标题“pickerview”表明我们将讨论这个组件的使用,而描述中提到的“省市区三级联动”则意味着...