簡體   English   中英

單擊tableview編輯按鈕時如何獲取索引路徑?

[英]How to get the indexpath when the tableview editbutton is clicked?

我有一個我有2 tableviewController的應用程序。 在第一個表格視圖中,我在表格視圖單元格中顯示數據庫中的值。 在導航欄的第一個表格視圖中,它們是一個編輯按鈕,因此當我單擊editbutton時,表格視圖將更改為編輯模式,以便可以編輯其所有單元格。

在第二個tableview控制器中,有一個開關按鈕。 當我將切換按鈕更改為關閉/打開firsttableview控制器上的圖像時,它也發生了變化,但是問題是當我單擊編輯按鈕並轉到secondviewcontroller並更改了第一行的切換按鈕時,它應該更改圖像第一行的圖像,但問題是它總是改變最后一行的圖像,而與行無關。

這是我的代碼:

這是我的firsttableviewcontroller的代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        mimageButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, 30, 30)];
        [mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
        [mimageButton setTag:indexPath.row];
        [mimageButton setUserInteractionEnabled:NO];
        [cell.contentView addSubview:mimageButton];

    }
    cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    al = [appDelegate.snoozearray objectAtIndex:indexPath.row];
    cell.mTimeLbl.text = al.Alarmtime;
    cell.mMessageLbl.text = al.AlarmMessage;
    cell.mRepeatLbl.text = [NSString stringWithFormat:@"Every%@", al.adays];
    cell.mPenalyLbl.text = [NSString stringWithFormat:@"Snooze:%@",al.Penaltytype];
    cell.accessoryType = UITableViewCellAccessoryNone;

     return cell;
}

這是我的setediting代碼。 當我單擊編輯按鈕時,將執行以下操作:

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

    appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication] delegate];
    [super setEditing:editing animated:animated];
    [mTableView setEditing:editing animated:YES];



        if (editing)
    {
        self.navigationItem.rightBarButtonItem = nil;
        [self.mTableView reloadData];

    }
    else {
        appDelegate.changeimagetype = [[NSUserDefaults standardUserDefaults]boolForKey:@"boolchange"];
        if (appDelegate.changeimagetype == YES)
        {

            [mimageButton setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];

        }
        else if (appDelegate.changeimagetype == NO) 
        {

            [mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];

        }
        self.navigationItem.rightBarButtonItem = addButton;
        if (appDelegate.snoozearray)
        {
            [appDelegate.snoozearray release];
        }
        appDelegate.snoozearray = [[NSMutableArray alloc]init]; 
        [Alarm getInitialDataToDisplay:[appDelegate getDBPath]];
        [self.mTableView reloadData];

}

}

這是我的第二個控制器的代碼,其中存在switchbutton,當我更改此switchbutton時,在firsttablecontroller上同時更改圖像。

- (void)viewWillAppear:(BOOL)animated 
{
    [self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    if (app.changeimagetype)
    {
        [switchControl setOn:NO];
    }
    else {
        [switchControl setOn:YES];
    }
    [self.tableView reloadData];

    [super viewWillAppear:animated];
}



-(void)switchChanged:(id)sender
{
    userdefaults = [NSUserDefaults standardUserDefaults];
    BOOL switchchng;
    if (switchControl.on)
    {   switchchng = NO;
        [userdefaults setBool:switchchng forKey:@"boolchange"];
        [switchControl setOn:YES animated:YES];
    }
    else {
        switchchng = YES;
        [userdefaults setBool:switchchng forKey:@"boolchange"]; 
        [switchControl setOn:NO animated:YES];
    }
    [userdefaults synchronize];

}

在下面的代碼中,您每次都將mimageButton設置為新值(針對您創建的每個新單元格)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        mimageButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, 30, 30)];
        [mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
        [mimageButton setTag:indexPath.row];
        [mimageButton setUserInteractionEnabled:NO];
        [cell.contentView addSubview:mimageButton];
    } ...

我認為您在這里更改了圖像:

if (appDelegate.changeimagetype == YES)
{
  [mimageButton setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];
}
else if (appDelegate.changeimagetype == NO) 
{
  [mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
}

在這種情況下, mimageButton很可能是最后一個單元格的按鈕(因為這是初始化的最后一個單元格,每個單元格都有唯一的重用標識符)。

相反,您應該像這樣更改圖像(其中tableview是您的第一個tableview):

TAlarmCell *cell =(TAlarmCell *) [tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithRow:0 inSection:0];
UIButton *iButton = (UIButton *)[cell.contentView.subviews lastObject]; // as @vakio mentioned below, it will be safer to use tags to fetch the right subview, 'lastObject' might return the wrong view in some situations, depending on what TAlarmCell does internally
 if (appDelegate.changeimagetype == YES)
{
  [iButton setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];
}
else if (appDelegate.changeimagetype == NO) 
{
  [iButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
}

無論如何,如果可能的話,您應該重新使用單元格( NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];導致這些單元格不被重用,從而為每行),並具有一個數組,其中對象代表單個單元格(例如:NSNumber(BOOL)的數組,其中每個NSNumber(BOOL)告訴您是否應該顯示單元格圖像)。

Table View編程指南可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM