gpt4 book ai didi

以编程方式选择时 UITableViewCell 变黑

转载 作者:行者123 更新时间:2023-12-02 21:50:06 28 4
gpt4 key购买 nike

我想知道为什么当我将选定的属性设置为 ON 时,这段代码会给我黑色的 UITableViewCells。如果我这样做,单元格的内容就会完全变黑,我不知道为什么。

这是代码

//
// TableViewAdapter.m
// TableviewScanMode
//
// Created by Nick Overdijk on 8/26/10.
// Copyright 2010 Nick Overdijk. All rights reserved.
//

#import "TableViewAdapter.h"
#import "Model.h"

@implementation TableViewAdapter

@synthesize model;

- (id) initWithModel: (Model*) model {
self = [super init];
if(self != nil){
self->model = [model retain];
}

return self;
}

- (void) dealloc {
[model release];
[super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[model cellData] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[model cellData] objectAtIndex: section] count];
}

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

static NSString *CellIdentifier = nil;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [[[model cellData] objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];

if(indexPath.row == [[model currentSelected] row] && indexPath.section == [[model currentSelected] section]){
cell.selected = YES;
} else {
cell.selected = NO;
}

return cell;
}

@end

//
// RootViewController.m
// TableviewScanMode
//
// Created by Nick Overdijk on 8/24/10.
// Copyright Nick Overdijk 2010. All rights reserved.
//

#import "RootViewController.h"
#import "Model.h"
#import "TableViewAdapter.h"


@implementation RootViewController


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
model = [[Model alloc] init];
[model addObserver:self
forKeyPath:@"updatedIndexPaths"
options:NSKeyValueObservingOptionNew
context:NULL
];

[model startSelectionRotation];

adapter = [[TableViewAdapter alloc] initWithModel: model];
self.tableView.dataSource = adapter;

[super viewDidLoad];
}

- (void)dealloc {
[adapter release];
[model release];
[super dealloc];
}

#pragma mark -
#pragma mark KVO updates
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSArray * reloadThese = [change objectForKey: NSKeyValueChangeNewKey];
[self.tableView reloadRowsAtIndexPaths: reloadThese withRowAnimation: UITableViewRowAnimationFade];
}

#pragma mark -
#pragma mark Table view delegate

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


@end

如果您需要更多代码,请大声喊叫。 :)

提前非常感谢,尼克

最佳答案

我遇到了同样的问题,并通过将 cell.selected = YES 移动到 tableView:willDisplayCell:forRowAtIndexPath 来修复它。反而。

我认为这可能与 UITableViewCell 文档底部关于更改背景颜色需要使用 tableView:willDisplayCell:forRowAtIndexPath 的注释有关(大概是 selected 设置背景颜色)。

关于以编程方式选择时 UITableViewCell 变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3576199/

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