我有一个应用程序,我有2个tableviewController。在第一个tableview中,我在tableview单元格中显示数据库中的值。在导航栏的第一个表视图中,它们是一个编辑按钮,这样当我点击编辑按钮时,我的tableview会切换到编辑模式,以便可以编辑其所有单元格。
在第二个tableview控制器中有一个开关按钮。当我将开关按钮更改为关/开时,第一个视图控制器上的图像也会改变,但问题是当我单击编辑按钮并转到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;
}
这是我的代码。当我单击编辑按钮时,将执行此操作:
-(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];
}
}
这是我的第二个控制器鳕鱼,其中存在开关按钮,当我更改此开关按钮时,同步图像在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 :(得分: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];
导致单元格不被重用,为每一行创建一个单独的单元格),并且有一个数组,其中的对象代表一个单元格(例如:array NSNumber(BOOL),其中每个NSNumber(BOOL)告诉您是否应显示单元格图像。
Table View Programming Guide可能会有所帮助。