我有一个UITableView
在xib
.
- 设置了委托和数据源出口
numberOfSectionsInTableView
在那儿numberOfRowsInSection
在那儿cellForRowAtIndexPath
被调用
我可以设置单元格的背景颜色,它工作正常。但是,设置 的textLabel
不起作用UITableViewCell
。
这是我的代码:
...
#pragma mark -
#pragma mark - UITableView datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[[PropertyManager sharedManager] fetchedResultsController] fetchedObjects] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Property *property = [[[PropertyManager sharedManager] fetchedResultsController] objectAtIndexPath:indexPath];
//This DOES prints to the console properly
NSLog(@"%@", property.descriptionShort);
[cell.textLabel setText:property.descriptionShort];
[cell.textLabel setTextColor:[UIColor blackColor]];
return cell;
}
#pragma mark -
#pragma mark -
- (void)filterToUsersLocation {}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
I tried with a custom UITableViewCell that didn't work either
[self.table registerClass:[PropertyCustomCell class] forCellReuseIdentifier:cellIdentifier];
[self.table registerNib:[UINib nibWithNibName:@"PropertyCustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
*/
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.table reloadData];
}
...
我的设置中唯一不同的是,包含此 UITableView 的 UIViewController 正在作为 childViewController 添加到父 UIViewController。这让我一开始相信这是问题所在,但由于设置单元格的背景颜色可以正常工作,我不相信这是一些 UIView 覆盖它的问题。
这是一些屏幕:
基本画面:
将背景颜色设置为蓝色时的屏幕:
编辑
我添加了一些日志语句,这让事情变得更加混乱:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Property *property = [[[PropertyManager sharedManager] fetchedResultsController] objectAtIndexPath:indexPath];
//This DOES prints to the console properly
NSLog(@"%@", property.descriptionShort);
//Prints out __NSCFString properly
NSLog(@"%@", [property.descriptionShort class]);
cell.backgroundColor = [UIColor blueColor];
[cell.textLabel setText:property.descriptionShort];
[cell.textLabel setTextColor:[UIColor blackColor]];
//Prints out correctly
NSLog(@"cellTextLabel = %@", cell.textLabel.text);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Prints out correctly
NSLog(@"cell textLabel = %@", cell.textLabel.text);
}
<UITableView: 0x151c8c00; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1469e340>; layer = <CALayer: 0x1469d9c0>; contentOffset: {0, 0}>
| <UITableViewWrapperView: 0x1469eab0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x1469eba0>>
| | <UITableViewCell: 0x1462b4e0; frame = (0 528; 320 44); text = '
Grand 6 Bd Home on...'; autoresize = W; layer = <CALayer: 0x1462b670>>
| | | <UITableViewCellScrollView: 0x1462bba0; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1462b3b0>; layer = <CALayer: 0x1462bd70>; contentOffset: {0, 0}>
| | | | <UITableViewCellContentView: 0x1462bf20; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x1462c5f0>; layer = <CALayer: 0x1462bf90>>
| | | | | <UILabel: 0x1462c740; frame = (15 0; 290 43.5); text = '
. . .
. . .
. . .
| <UIImageView: 0x1466dc10; frame = (0 564.5; 320 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1466dca0>>
| <UIImageView: 0x1469e030; frame = (316.5 561; 3.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1466dcd0>>