我有一个自定义的UITableView Cell,我选择了一个特定的单元格并转到另一个ViewController,当我回到第一个视图控制器时,单元格的选定状态不可见。在从另一个viewControllers导航后,我将如何改变单元格的选定状态?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [timeSet count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
timeSettingCell *newCell = nil;
newCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(newCell == nil)
{
NSLog(@"newCell ===================");
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"timeSettingCell" owner:self options:nil];
newCell = [ nibViews lastObject];
}
newCell.timeLabel.text=[timeSet objectAtIndex:indexPath.row];
if (newCell.selected==YES) {
newCell.highlighted=YES;
newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
}
else {
newCell.highlighted=NO;
newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
}
return newCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
value=[[time1 objectAtIndex:indexPath.row]integerValue];
SettingsViewController *settings=[[SettingsViewController alloc]initWithNibName:nil bundle:nil andCounterValue:value];
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
index=indexPath.row;
[settings release];
}
-(void)viewWillAppear:(BOOL)animated{
}
谢谢
答案 0 :(得分:2)
这样,它会正常工作......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
答案 1 :(得分:1)
我认为您应该使用创建为单例,ivar或NSUserDefault的NSArray记住您的单元格状态(选择与否),然后在cellForRowAtIndexPath
中读取其状态。
编辑:示例代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do what you want
// This save the index of your selected cell on disk
[[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create your cell
// Here you check if the cell should be selected or not
if (indexPath.row == [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedCell"]) {
newCell.highlighted=YES;
newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
} else {
newCell.highlighted=NO;
newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
}
return aCell;
}
NSUserDefaults
可用于在您的设备上保存数据并在以后检索它们。这意味着即使关闭并重新打开您的应用后,您也可以检查您的手机状态。
要使用NSUserDefaults
,您需要为项目添加一个Settings.bundle。看看NSUserDefaults Class Reference