2012-08-11 76 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

相關問題