37

iPad デバイスで実行している場合、iOS 7 で静的 UITableViewCells の背景色を変更できません。これは、次のセットアップで簡単に確認できます。

  • 2 つのストーリーボードを使用して、Xcode 5 で新しいユニバーサル プロジェクトを作成します。
  • 各ストーリーボードには、1 つのコントローラー (テーブル ビュー コントローラー) のみを配置し、最初のコントローラーとして設定します。
  • コントローラー/ストーリーボードの両方のテーブル ビューにいくつか (たとえば 3 つ) の静的セルを配置します。
  • Interface Builder で各静的セルの背景色を異なる色に設定します (私は赤、緑、および透明色を使用しています)。

ここで、iPhone および iPad シミュレーター (iOS 7) でアプリを実行します。

iPhone シミュレーターでは、すべて問題ありません。
一方、iPad シミュレーターでは、すべてのセルが白く着色されています。

セルのInterface Builderでランタイムプロパティを設定することにより、iPadが正しく動作するように強制しようとしました:

  • backgroundColor色をクリアにする
  • contentView.backgroundColor色をクリアにする
  • backgroundView無に

しかし、何も役に立ちません。実際には、ランタイム プロパティを設定するcontentView.backgroundColorとセルの色が変わりますが、クリア カラーでは機能しません (つまり、その背後に白で色付けされた別のビューがあることを意味します)。

iOS の同じバージョンの 2 つのデバイスで異なる結果が得られるのは非常に奇妙です。他の誰かがこのバグを確認できますか?

誰かがこの問題の解決策を持っていますか、または唯一の方法は、動的プロパティ + で色を設定することcellForRowAtIndexPathですか? 問題の性質は静的であるため、静的セルを使用したいと思います。

ps ランタイム プロパティをクリア カラーに設定するのを忘れていたことに気付きbackgroundView.backgroundColorました。現在、Mac にアクセスできません。多分それはトリックを行うでしょう。

4

6 に答える 6

10

Appleのドキュメントによると:

backgroundView定義済みセルまたはカスタム セルを使用するかどうかに関係なく、プロパティを使用するか、継承されたプロパティを変更することにより、セルの背景を変更できbackgroundColorます。iOS 7 では、セルの背景はデフォルトで白です。以前のバージョンの iOS では、セルは囲んでいるテーブル ビューの背景色を継承します。セルの背景色を変更する場合tableView:willDisplayCell:forRowAtIndexPath:は、テーブル ビュー デリゲートのメソッドで行います。

そのため、 でどの色を設定してもtableView:cellForRowAtIndexPath:、後で iOS 7 によって白に変更されます。にセットするだけtableView:willDisplayCell:forRowAtIndexPath:。私のために完璧に働きました。

于 2013-10-08T04:09:04.540 に答える
2

この問題を解決できる唯一の方法は、ストーリーボードを使用する代わりにプログラムでテーブルを作成することでした。参考までに、私のソリューションを投稿します。それが誰にも役立つことを願っています。

uitableviewcontroller を uiviewcontroller に置き換えました。

次にこれを追加しました:

ヘッダファイル

#import <UIKit/UIKit.h>

@interface LtHomeViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *mFiles;
NSArray *mPics;
}

@end

そしてモジュールファイル

#import "LtHomeViewController.h"
#import "LtHomeToolbar.h"
#import "LtHomeCustomCell.h"

@interface LtHomeViewController () <LtHomeToolbarDelegate>

@end

@implementation LtHomeViewController
{
LtHomeToolbar *mainToolbar;
UITableView *theListView;
}

#define TOOLBAR_HEIGHT 64.0f

-(UIStatusBarStyle)preferredStatusBarStyle{
 return UIStatusBarStyleLightContent;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

CGRect toolbarRect = self.view.bounds; // View controller's view bounds
toolbarRect.size.height = TOOLBAR_HEIGHT;

mainToolbar = [[LtHomeToolbar alloc] initWithFrame:toolbarRect]; // At top
mainToolbar.delegate = self;

[self.view addSubview:mainToolbar];

//
mFiles = [[NSArray alloc] initWithObjects:@"../Lumina.app/lumina0.pdf", @"../Lumina.app/lumina8.pdf", @"../Lumina.app/lumina9.pdf", @"../Lumina.app/lumina10.pdf", nil];
mPics = [[NSArray alloc] initWithObjects:@"vol0.jpg", @"vol8.jpg", @"vol9.jpg", @"vol10.jpg", nil];
//
CGRect tableViewFrame = self.view.bounds;
tableViewFrame.origin.y = TOOLBAR_HEIGHT;
tableViewFrame.size.height = self.view.bounds.size.height - TOOLBAR_HEIGHT;

theListView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
theListView.delegate = self;
theListView.dataSource = self;

theListView.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];


[self.view addSubview:theListView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
   return 1;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
   return [mFiles count];
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellIdentifier = @"HomeCell";

   // Similar to UITableViewCell, but
   LtHomeCustomCell *cell = (LtHomeCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[LtHomeCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

cell.image.image = [UIImage imageNamed:[mPics objectAtIndex:indexPath.row]];

NSString *magName = [[mFiles objectAtIndex:indexPath.row]stringByReplacingOccurrencesOfString:@"../Lumina.app/" withString:@""];
magName = [magName stringByReplacingOccurrencesOfString:@"lumina" withString:@""];
magName = [magName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
magName = [[@"Triathlon LUMINA " stringByAppendingString:magName]stringByAppendingString:@"号"];

cell.descriptionLabel.text = magName;

return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


@end

この行だけが役立つかどうかはわかりませんが、もっと必要なものが必要なため、テーブルを手動で作成するアプローチを取りました

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    cell.backgroundColor = [UIColor colorWithRed:(116/255.0) green:(167/255.0) blue:(179/255.0) alpha:1.0];
}

デビッド

于 2013-09-27T02:22:30.393 に答える
1

代わりに ContentView の背景色を変更すると機能します。

于 2014-08-23T00:58:18.077 に答える
1

セルの背景色をオーバーライドするもっと簡単な方法があります。UITableViewCell をサブクラス化し、静的テーブルビュー セルのクラスとして設定します。次にオーバーライドします。

-(void) willMoveToSuperview:(UIView *)newSuperview  {
    self.backgroundColor = [UIColor ...];
}

where [UIColor ...] = 好きな色 (例: [UIColor clearColor] など);

于 2014-07-28T16:44:00.657 に答える