Iphone can';t更改UITableViewCell背景色
我的Iphone can';t更改UITableViewCell背景色,iphone,objective-c,ios,xcode,uitableview,Iphone,Objective C,Ios,Xcode,Uitableview,我的UITableViewCell适合UITableView,但在进入编辑样式模式时,我无法更改背景色。。我尝试了网络上的一切,搜索了一整天,但仍然无法修复(我也搜索了StackOverflow)。请帮帮我 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"C
UITableViewCell
适合UITableView
,但在进入编辑样式模式时,我无法更改背景色。。我尝试了网络上的一切,搜索了一整天,但仍然无法修复(我也搜索了StackOverflow)。请帮帮我
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (MainTableViewCell *)view;
}
}
}
尝试设置cells backgroundColor属性,为我工作
cell.backgroundColor=[UIColor blueColor]代码>要完全控制单元格的自定义,我宁愿覆盖单元格视图
为单元格视图创建一个新类,该类由UITableView调用
稍后当我到达我的工作电脑时,如果你还没有找到答案,我会发布一些示例代码
一旦你看到它是如何工作的,那就很容易了
您还可以为您的单元格背景放置一个图像,并在单元格的自定义位置放置不同的标签和图像、按钮、文本字段
编辑>>
密码![仅仅为了改变背景就太过分了,但是如果你真的想定制你的手机,这是最好的选择!]
在你的手机里
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
UILabel *_kLabel;
UILabel *_dLabel;
}
@property (nonatomic, retain) UILabel *kLabel;
@property (nonatomic, retain) UILabel *dLabel;
- (void) initLabels;
@end
在你的ViewController.m中
YourViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
享受!!
;) 尝试在tableView:willDisplayCell:
方法中自定义单元格,如下所示:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
请试试这个,它对我有用
UIView *bgView = [[UIView alloc] initWithFrame:cell.frame];
bgView.backgroundColor = [UIColor greenColor];
cell.backgroundView = bgView;
[bgView release];
您需要更改tableView单元格的contentView,而不是单元格的背景视图
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.contentView.backgroundColor = [UIColor redColor];
}
正如@Westley所提到的
您需要更改tableView单元格的contentView,而不是单元格的背景视图
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.contentView.backgroundColor = [UIColor redColor];
}
这是你应该做的:
if(index == conditionYouWant)
{
cell.contentView.backgroundColor = [UIColor whiteColor];
}
else
{
cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0];
}
希望它能帮你们解决问题确保您没有在情节提要中设置内容视图的背景色。如果您这样做了,更改代码中的cell.backgroundColor将不会有任何区别,而且您将在混乱中不断重复(就像我所做的那样)。在swift 3中,您可以使用
cell.backgroundcolor = UIColor.white
@YossiTsafar检查编辑!,以image.png为背景,我觉得更灵活,@YossiTsafar,不用担心,如果你觉得有用的话,记得向上投票,选择正确。
cell.backgroundcolor = UIColor.white