2012-02-09 119 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

相关问题