我有一個應用程序,我有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];
}
這是正確的,但'cell.contentView'應該是'[cell.contentView viewWithTag:indexPath.row]'。另外,將標記設置爲indexPath.row是錯誤的,因爲所有視圖都以標記0開始,所以第0行將具有標記0. – vakio 2012-02-09 10:30:48
哦..對,因爲某些原因,我讀了'cell.contentView = mimageButton;'(這不會是'自從只讀以來一直工作)。將更新,謝謝。 – 2012-02-09 10:34:13
我應該在TAlarmCell * cell =(TAlarmCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexpathWithRow:0 inSection:0]; – Rani 2012-02-09 10:43:15