カスタム背景画像付きのiPhone UITableView PlainStyle-コードで「完全に」実行
-
08-07-2019 - |
質問
私はあちこちにいましたが、静的なバックグラウンドの問題がある UITableView
はよく文書化されているようですが、簡単な解決策はありませんか?
次のように、完全にコードで TableViews
を構築しています:
UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];
ウィンドウは、アプリデリゲートでのメインの UIWindow
ビルドです。ここから、いくつかの異なる TableViews
( navigationController
で制御)を作成する必要があります。一部は fetchedResultsControllers
、カスタムセルなどを使用します。これは、コードとIBの間でカスタマイズを広げたり、6つ以上の異なるNibを構築および保守する必要があるため、nibを使用せずに完全にコードで行うことを好みます。
tableViewController
クラスが独自の背景画像を設定する実用的な例を見つけることができません。 TableViews
のいずれかでこれを行うと( UITableViewController
を拡張する):
self.tableView.backgroundColor = backgroundColor;
もちろん、tableViewの背景色を取得します(偶然にもセルを色付けします。セルの色は tableView
から色を継承すると思いますか?)私の細胞は上下にスライドします。 「背景画像」ではありませんユーザーのジェスチャーで上下にスライドします。
GroupedStyle tableViewが提供するものとまったく同じですが、PlainStyle tableView :)で、IBではなくコードを使用して実行されます。
Table Viewの背景色をクリアしてから、設定時にCellsの色を設定して、透明にならないようにする必要があると思います。そして、どういうわけか「こっそり」 tableViewインスタンス内からtableViewビューの下に背景画像がありますか?
これをどうすればよいのか、最適な解決策は、viewDidLoadまたはTableViewController内のその他の関数でこれを実行し、すべてのカスタマイズを1か所に保持することです。
誰かが私を助けてくれることを願っています、私はすべて「グーグルアウト」です:)ありがとう!
解決
コントローラーを UITableViewController
ではなく、 UIViewController
として設定する必要があります。次に、 tableview
をプログラムで背景 imageView
の上に追加します。
@interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
...
UITableView *tableView;
...
}
@property (nonatomic, retain) UITableView *tableView;
@end
@implementation SomeController
@synthesize tableView;
...
- (void)loadView {
[super loadView];
UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[v setImage:[UIImage imageNamed:@"table_background.png"]];
[self.view addSubview:v];
self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}
...
@end
他のヒント
ターゲットを設定している場合、フープをジャンプすることは不要です&gt; iOS 3.2。新しい backgroundView プロパティでUIImageViewを直接設定します。例:
// In your UITableViewController subclass
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = view;
}
OK、今は実行中です:) tableViewにセルが入力されていなかったため、見つけたものをブレークポイントする TableViewDataSourceとTableViewDelegateを実装していても、これはメインビューのみにあったため、tableviewのデリゲートとデータソースを= selfに設定する必要がありました。 これに対する答えを探している他の人のために、Coneybearesの助けが得られた方法があります:
- (void)loadView {
[super loadView];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];
[self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
コニーベアに感謝します。
もうクラッシュせず、背景画像が完璧になります(上部にあるnavigationControllerと共に) ただし、まだ表示されるtableViewはありませんか?背景画像とnavigationControllerBarのみ:
これは私の実装です:
- (void)loadView {
[super loadView];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];
[self.tableView = [[UIView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Hello, World";
return cell;
}
// commit edit、didSelectRow、memory&#8230;など。
フォーラムは、一度に.mファイル全体を作成することはできませんでした。 エラーを示すのに役立つ可能性のある何かを省いたかどうか教えてください。
多分それはレイヤーの順序だと思い、これを運なしで試しました:
[self.view sendSubviewToBack:imageView];
明白なものを見逃したことを願っています。
お時間をいただきありがとうございます:)
ヒント:
-
UISplitViewController
を使用している場合:splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
-
UINavigationController
を使用する場合:navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
-
UISplitViewController
とUINavigationController
の両方を使用する場合:navigationController.view.backgroundColor = [UIColor clearColor]; splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
UINavigationController
または UISplitViewController
の上にある、見たい UIView
の背景色を必ず< code> [UIColor clearColor] 。