使用空白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
}
}
//當前選中行設為非選中
[self.membersListView deselectRowAtIndexPath:membersListView.indexPathForSelectedRow animated:YES];