2012-02-09 120 views
0

我有一個應用程序,我有2個tableviewController。在第一個tableview中,我在tableview單元格中顯示來自我的數據庫的值。在導航欄的第一個tableview中,它們是一個編輯按鈕,這樣當我點擊editbutton時,我的tableview就會變爲編輯模式,以便可以編輯它的所有單元格。如何在點擊tableview editbutton時獲取indexpath?

在第二個tableview控制器中有一個開關按鈕。當我將開關按鈕改變爲off/on時,firsttableview控制器上的圖像也會改變,但問題是當我點擊編輯按鈕並轉到第二個控制器並更改第一行的開關按鈕時,它應該改變圖像的第一行,但問題是它總是改變最後一行的圖像,而不管行。

這是我的代碼:

這是我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]; 

} 

} 

這是我secondcontroller鱈魚哪裏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]; 

} 

回答

0

在下面的代碼,你設定每次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是你的第一個實現代碼如下):

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(布爾值)告訴您是否顯示單元格圖像)。

Table View Programming Guide可能會對您有所幫助。

+1

這是正確的,但'cell.contentView'應該是'[cell.contentView viewWithTag:indexPath.row]'。另外,將標記設置爲indexPath.row是錯誤的,因爲所有視圖都以標記0開始,所以第0行將具有標記0. – vakio 2012-02-09 10:30:48

+0

哦..對,因爲某些原因,我讀了'cell.contentView = mimageButton;'(這不會是'自從只讀以來一直工作)。將更新,謝謝。 – 2012-02-09 10:34:13

+0

我應該在TAlarmCell * cell =(TAlarmCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexpathWithRow:0 inSection:0]; – Rani 2012-02-09 10:43:15

相關問題