问题描述
限时送ChatGPT账号..我想知道为什么当我将 selected 属性设置为 ON 时,这段代码会给我黑色的 UITableViewCells.如果我这样做,单元格的内容会变成全黑,我不知道为什么.
这是代码
<代码>////TableViewAdapter.m//表视图扫描模式////由 Nick Overdijk 于 10 年 8 月 26 日创建.//版权所有 2010 Nick Overdijk.版权所有.//#import "TableViewAdapter.h"#import "Model.h"@implementation TableViewAdapter@合成模型;- (id) initWithModel: (Model*) 模型 {self = [超级初始化];如果(自我!=零){self->model = [模型保留];}回归自我;}- (无效)dealloc {【模型发布】;[超级dealloc];}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {返回[[模型单元数据]计数];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [[[model cellData] objectAtIndex: section] count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {静态 NSString *CellIdentifier = nil;UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(单元格 == 零){cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault重用Identifier: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;} 别的 {cell.selected = NO;}返回单元格;}@结尾////RootViewController.m//表视图扫描模式////由 Nick Overdijk 于 10 年 8 月 24 日创建.//版权所有 Nick Overdijk 2010.保留所有权利.//#import "RootViewController.h"#import "Model.h"#import "TableViewAdapter.h"@implementation 根视图控制器#pragma mark -#pragma mark 视图生命周期- (void)viewDidLoad {模型 = [[模型分配] init];[模型 addObserver:selfforKeyPath:@"updatedIndexPaths"选项:NSKeyValueObservingOptionNew上下文:NULL];[模型开始选择旋转];适配器 = [[TableViewAdapter alloc] initWithModel: 模型];self.tableView.dataSource = 适配器;[超级viewDidLoad];}- (void)dealloc {【适配发布】;【模型发布】;[超级dealloc];}#pragma mark -#pragma mark KVO 更新- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {NSArray * reloadThis = [change objectForKey: NSKeyValueChangeNewKey];[self.tableView reloadRowsAtIndexPaths: reload这些 withRowAnimation: UITableViewRowAnimationFade];}#pragma mark -#pragma mark 表视图委托- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}@结尾
如果您需要更多代码,请大喊.:)
非常感谢,尼克
解决方案我遇到了同样的问题并通过将 cell.selected = YES
移动到 tableView:willDisplayCell:forRowAtIndexPath 代替.
我认为这可能与 UITableViewCell 文档底部关于需要使用 tableView:willDisplayCell:forRowAtIndexPath 更改背景颜色的注释有关(大概是 selected
设置背景颜色).>
I was wondering why this code would give me black UITableViewCells when I set the selected property ON. If I do that, the content of the cell turns completely black, and I have no idea why.
Here's the code
//
// 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
If you need more code, shout. :)
Many thanks in advance, Nick
解决方案I was having the same problem and fixed it by moving the cell.selected = YES
into tableView:willDisplayCell:forRowAtIndexPath instead.
I think it might be related to the note at the bottom of the UITableViewCell docs about changes to background color requiring use of tableView:willDisplayCell:forRowAtIndexPath (presumably selected
sets the background color).
这篇关于以编程方式选择时 UITableViewCell 变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论