2012-08-11 78 views
1

编辑模式时,我可以在单元格内隐藏我的按钮吗?我的单元格由数组填充,保存为plist。这是我的代码如何在UITableViewCell中编辑模式时隐藏按钮

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [titleArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"the indexpath.row = %i",indexPath.row); 

    UITableViewCell *cell = [[UITableViewCell alloc]init]; 

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:CGRectMake(250.0, 25.0, 30.0, 30.0)]; 
    [button setTitle:@"!" forState:UIControlStateNormal]; 
    [button setTitle:@"X" forState:UIControlStateSelected]; 
    [button setTag:indexPath.row + 100]; 
    [button addTarget:self action:@selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside]; 

    // my label code 

    UIImageView *bgCell = [[UIImageView alloc]initWithFrame:CGRectMake(0,0 , 320, 80)]; 
    bgCell.image = [UIImage imageNamed:@"BG.jpeg"]; 

    [cell.contentView addSubview:button]; 
    // other cell contentView addSubview everything 

    return cell; 
    [tableView reloadData]; 
} 

这是我的编辑按钮

- (IBAction)editBtn:(id)sender 
{ 
    UIButton *editBtn = (UIButton *)sender; 
    editBtn.selected = !editBtn.selected; 

    if (editBtn.selected) 
    { 
     button.hidden = YES; 
     [self.tableView setEditing:YES animated:YES]; 
    } 
    else 
    { 
     button.hidden = NO; 
     [self.tableView setEditing:NO animated:YES]; 
    } 
} 

与上面的代码中,在最后一个单元格我的按钮隐藏而已,只是说,如果我有3个单元。如何在编辑模式下隐藏所有按钮隐藏?我希望所有的按钮都隐藏起来。下面是我的onoffBtn。

- (IBAction)onoffBtn:(id)sender 
{ 
    tempIndexPath = [_tableView indexPathForCell:(UITableViewCell*)[[sender superview] superview]]; 
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:tempIndexPath]; 
    UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100]; 

    onoffBtn.selected = !onoffBtn.selected; 
    if (onoffBtn.selected) 
    { 
     // start 
    } 
    else 
    { 
     // stop 
    } 
} 

请教我更多。谢谢。

回答

1

你将不得不调用此方法对表的所有UiTableViewCells:

if (editBtn.selected) 
     { 

for (int row = 0, rowCount = [_iTableView numberOfRowsInSection:0]; row < rowCount; ++row) { 
     UITableViewCell *cell = [_iTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]]; 

     UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100]; 
    onoffBtn.hidden=YES; 
    } 
    } 
+0

对不起,即时通讯初学者,我真的不明白的地方是rowCount时和部分从自带?但至少我已经尝试过使用你的方法,但它仍然不能隐藏我的onoffBtn。 – Piyo 2012-08-11 09:04:09

+0

rowCount是总数量。的行中的行:0 {即rowCount = [_iTableView numberOfRowsInSection:0];}的tableView作为你的tableView只有一个section.You可以写在For循环之前。 – 2012-08-11 09:06:14

+0

嘿,不管你是谁,谢谢你教我。它只隐藏第一个单元格,但我认为我可以使用你的方法修复它。至少我明白了。再次感谢你。 – Piyo 2012-08-11 09:30:23

相关问题