这个问题是关于在 Objective-C 中渲染用户界面的组件。
每当表格单元格中的数据被认为有效时,我试图在表格单元格的右侧显示一条绿色条纹。
为了实现这一点,我决定创建一个名为 validatedBar 的 UIView 对象,作为自定义 UITableViewController 对象的一个组件,其位置和形状为沿着表格单元格右侧对齐的绿色细条。当表格单元格呈现到显示器上时,我检查 validatedBar 是否需要将其背景颜色设置为绿色而不是常规的白色。以下摘录希望能显示相关信息:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *scheduleItem = @"ScheduleItemTableCell";
NSLog(@"cellForRowAtIndexPath");
ScheduleItemTableCell *cell = (ScheduleItemTableCell *)[tableView dequeueReusableCellWithIdentifier:scheduleItem];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleItemTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
ScheduleListItem *item = [self.scheduleItems objectAtIndex:indexPath.row];
// Other detail being loaded has been removed for brevity.
if(item.validated == YES){
NSLog(@"Should set the validated bar for list item number %ld",(long)indexPath.row);
cell.validatedBar.backgroundColor = [UIColor greenColor];
}
return cell;
}
渲染单元格时,即使我看到日志中显示的输出,我认为应该出现的绿色条也不会出现。
我没有做什么?这个问题有什么我应该提供的不清楚的地方,或者有一个非常明显的解决方案吗?
最佳答案
我认为这是不正确的...
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleItemTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
如果不看,我认为此时您不能调用 LoadNibNamed。它可能对您有用,但绝对不是设计使然,更多的是偶然,因此,您的 View 无法正常工作,并且背景颜色没有按预期更改。
尝试这种方法...
NSString *CellIdentifier = @"ScheduleItemTableCell";
ScheduleItemTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ScheduleItemTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
你能看出这两个代码片段的区别吗? 改变这个,你的代码的其余部分应该可以工作。让我知道吗??
关于ios - 将 UIView 单元格组件的背景颜色更改为绿色不会将组件呈现为绿色,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202101/