gpt4 book ai didi

ios - 选择后永久更改tableView中的背景单元格

转载 作者:行者123 更新时间:2023-11-28 22:22:05 25 4
gpt4 key购买 nike

如何在选中后永久更改背景单元格?我所知道的是,通过使用 cell.selectedBackgroundView 它只会更改单元格背景一段时间,然后恢复正常。

即使在应用程序关闭后,我也希望它在选中时永久更改。我试图搜索解决方案,但找不到相关答案。

这是我的cellForRowAtIndexPath 方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

//set up selected cell background
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

}

//set up cell
CGFloat nRed= 0/255.0;
CGFloat nGreen=73.0/255.0;
CGFloat nBlue=144.0/255.0;
UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];

//set up cell text
cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
cell.textLabel.textColor=myColor;
cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];

//set icon image for cell
cell.imageView.image = [UIImage imageNamed:
[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"icon"]];

return cell;
}

下面是我的didSelectRowAtIndexPath 方法;

- (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

indexPath];
[self->tableView deselectRowAtIndexPath:indexPath animated:YES];

if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a1"]){

NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// saving an NSString
[defaults setObject:title_a1 forKey:@"title_a1"];
[defaults setObject:content_a1 forKey:@"content_a1"];

WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];

}else if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a2"]){

NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// saving an NSString
[defaults setObject:title_a2 forKey:@"title_a2"];
[defaults setObject:content_a2 forKey:@"content_a2"];

WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];

}else {

NSLog(@" other action key get! ");
}
}

最佳答案

您可以将一个属性添加到您的数据源数组,例如,命名为 BOOL isSelected。在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中更新 isSelected = YES。并添加以下内容

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

更新选定的单元格。在你的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中

if(data.isSelected){
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
}

代码在这里:

您需要向数组中的对象添加一个 bool 属性。例如,您添加

@property (nonatomic) BOOL isSelected; 到您的对象类的头文件。

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

//set up selected cell background
//cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
//cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

}

if (((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected){
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"blue_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
} else {
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_selected.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];

}

//set up cell
CGFloat nRed= 0/255.0;
CGFloat nGreen=73.0/255.0;
CGFloat nBlue=144.0/255.0;
UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nGreen blue:nBlue alpha:1];

//set up cell text
cell.textLabel.text = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
cell.textLabel.textColor=myColor;
cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 0.2 alpha: 1.0];

//set icon image for cell
cell.imageView.image = [UIImage imageNamed:
[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"icon"]];

return cell;
}


- (void)tableView:(UITableView *)tableView2 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

indexPath];
[self->tableView deselectRowAtIndexPath:indexPath animated:YES];

((YourObjectType *)[AryStoreknowItem objectAtIndex:indexPath.row]).isSelected = YES;

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];

if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a1"]){

NSString *title_a1 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *content_a1 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// saving an NSString
[defaults setObject:title_a1 forKey:@"title_a1"];
[defaults setObject:content_a1 forKey:@"content_a1"];

WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];

}else if ([[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"action"] isEqualToString:@"a2"]){

NSString *title_a2 =[[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *content_a2 = [[AryStoreknowItem objectAtIndex:indexPath.row] objectForKey:@"content"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// saving an NSString
[defaults setObject:title_a2 forKey:@"title_a2"];
[defaults setObject:content_a2 forKey:@"content_a2"];

WebBrowserViewController *webview=[[WebBrowserViewController alloc]initWithNibName:@"WebBrowserViewController" bundle:nil];
[self presentModalViewController:webview animated:YES];

}else {

NSLog(@" other action key get! ");
}
}

关于ios - 选择后永久更改tableView中的背景单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com