0

コードに Simple critical Problem が 1 つあります。

Editボタン(ナビゲーションバーにあります)を押したときのTableviewで、 UITableview のメソッドを編集する必要があります。My Cell にあるボタンとラベルを非表示にしたい。

コード :

このようにボタンを追加しています..

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    currentCell = nil;
    if (currentCell==nil)
    {
        currentCell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    BtnEmail=[UIButton buttonWithType:UIButtonTypeCustom];
    //BtnEmail = (UIButton *)[currentCell viewWithTag: 3];
    BtnEmail.frame=CGRectMake(265, 17, 25, 25);
    BtnEmail.tag=indexPath.row;
    //[BtnEmail setTitle:@"E-mail" forState:UIControlStateNormal];
    [BtnEmail setBackgroundImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
    [BtnEmail addTarget:self action:@selector(BtnEmail:) forControlEvents:UIControlEventTouchUpInside];
    [currentCell.contentView addSubview:BtnEmail];
    [currentCell.contentView bringSubviewToFront:BtnEmail];



    return currentCell;
}

私の編集ボタンは Declare like this

編集ボタン :

self.navigationItem.rightBarButtonItem = self.editButtonItem;

編集をクリックすると、 My this method will call が呼び出されます。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [listTableView setEditing:editing animated:animated];

    if(editing)
    {
        self.navigationItem.leftBarButtonItem.enabled = NO;
        //BtnEmail.frame=CGRectMake(355, 17, 25, 25);
        BtnEmail.hidden = TRUE;
    }
    else
    {
        self.navigationItem.leftBarButtonItem.enabled = YES;
        //BtnEmail.frame=CGRectMake(265, 17, 25, 25);
        BtnEmail.hidden = FALSE;
    }

    [super setEditing:editing animated:animated]; 
}

この場合、私の Last セル Button と Lable は、すべてを非表示にするわけではありません。Cell のすべての UIButton を非表示にする必要があります。

テーブルに3つのセルがある場合のように、最後のボタンのみを非表示にします..

ありがとう 。

4

2 に答える 2

3

条件付きチェックインを行うだけでcellForRow、編集が変更されたときにテーブルビューをリロードできます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // Do your other stuff

// Add this conditional check
if (tableView.editing)
{
    BtnEmail.hidden = YES;
}
else
{
    BtnEmail.hidden = NO;

}
[currentCell.contentView addSubview:BtnEmail];
[currentCell.contentView bringSubviewToFront:BtnEmail];

return currentCell;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{

[super setEditing:editing animated:animated];
[listTableView setEditing:editing animated:animated];

// Reload the table view
[listTableView reloadData];
// Do your other stuff
 }

ただし、カスタム セルを使用すると、そのボタンを簡単に作成できます。ここでは、ボタンを何度も追加しています。それ以外の場合は、コードを作成するボタンをif(cell == nil)ブロックに移動します

于 2013-02-26T11:28:13.930 に答える
0

それはあなたが宣言しているからです

BtnEmail.hidden = FALSE;

この場合、メソッドで最後に割り当てられたボタンcellForRowAtIndexPathです。次のようなことを試してください:

    if(editing)
        {
            self.navigationItem.leftBarButtonItem.enabled = NO;
            //BtnEmail.frame=CGRectMake(355, 17, 25, 25);
             For(int i=0; i< [tableArray count]; i++)
        {
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ;
            UIButton *btn=(UIButton *)[cell viewWithTag:i];
            btn.hidden = TRUE;
        }
        }
  else
    {
        self.navigationItem.leftBarButtonItem.enabled = YES;
            //BtnEmail.frame=CGRectMake(265, 17, 25, 25);
       For(int i=0; i< [tableArray count]; i++)
      {
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ;
            UIButton *btn=(UIButton *)[cell viewWithTag:i];
            btn.hidden = FALSE;
        }
        }
于 2013-02-26T11:13:02.110 に答える