اي فون UITableView PlainStyle مع صورة خلفية مخصصة - القيام به "تماما" في التعليمات البرمجية

StackOverflow https://stackoverflow.com/questions/1637738

سؤال

لقد كنت في كل مكان ، يبدو UITableView مع خلفية ثابتة المسألة موثقة جيدا, ولكن لا أحد على التوالي إلى الأمام الحل ؟ Im بناء بلدي TableViews تماما في رمز مثل هذا:

    UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
                        initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];

النافذة الرئيسية بلدي UIWindow بناء علي في التطبيق مندوب.من هنا أنا بحاجة إلى بناء عدد قليل من مختلف TableViews (التي تسيطر عليها navigationController) ، مع بعض fetchedResultsControllers, مخصص الخلايا وهلم جرا.أنا أفضل أن تفعل هذا تماما في التعليمات البرمجية ، وليس باستخدام بنك الاستثمار القومي مثل هذا من شأنه أن يؤدي إما وجود التخصيص تنتشر بين كود و IB أو الحاجة إلى بناء والحفاظ على 6+ حبيبات مختلفة.

أنا ببساطة لا يمكن العثور على سبيل المثال العمل فيها tableViewController فئة مجموعات خاصة انها صورة الخلفية.إذا كنت تفعل هذا داخل واحدة من بلدي TableViews (تمديد UITableViewController):

self.tableView.backgroundColor = backgroundColor;

أنا بالطبع الحصول على tableView خلفية ملونة (والتي بالمناسبة الألوان الخلية كما أعتقد الخلية يرث لونها من tableView?) ولكن أتمنى أن يكون صورة خلفية ساكنة هذه الخلايا الشريحة صعودا وهبوطا على رأس.وليس "صورة الخلفية" أن ينزلق صعودا وهبوطا مع المستخدمين الإيماءات.بالضبط ما GroupedStyle tableView العروض ، ولكن في PlainStyle tableView:) ..و يتم ذلك باستخدام رمز, لا IB.

أعتقد أن مسح لون الخلفية من عرض الجدول ، ثم تعيين خلايا اللون عند تكوين لهم حتى لا تتحول شفافة.ثم بطريقة أو بأخرى "التسلل" خلفية الصورة أدناه tableView عرض من داخل tableView سبيل المثال ؟

كيف يمكنني التوجه نحو هذا ، فإن أفضل حل هو أن تكون قادرة على القيام بذلك في viewDidLoad أو أي وظيفة أخرى داخل TableViewController ، للحفاظ على كل ما عندي من التخصيص في مكان واحد.

آمل شخص يمكن أن تساعد لي ، أنا 'بحثت خارج' :) شكرا!

هل كانت مفيدة؟

المحلول

وأنت تحتاج إلى إعداد تحكم ك UIViewController، وليس UITableViewController. قم بإضافة 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

نصائح أخرى

كل هذا القفز من خلال الأطواق غير ضرورية إذا كنت تستهدف> دائرة الرقابة الداخلية 3.2، حيث يمكنك استخدام جديد <لأ href = "http://developer.apple.com/library/ios/documentation/UIKit/Reference /UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/backgroundView "يختلط =" noreferrer "> الملكية backgroundView لتحديد UIImageView مباشرة، على سبيل المثال:

// In your UITableViewController subclass
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    self.tableView.backgroundView = view;
}  

حسنا، الآن كان يعمل :) كان لي tableView لا بالسكان مع خلايا بلدي، لذلك breakPointing من خلال شيء وجدت هذا على الرغم من أنني نفذت TableViewDataSource وTableViewDelegate، وكان هذا فقط في شاشة العرض الرئيسية، وكنت بحاجة إلى تعيين مندوب ومصدر بيانات من tableview إلى = النفس. للآخرين الذين يسعون الى الإجابة على هذا هنا هو الأسلوب لأنه انتهى مع 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;

و}

وبفضل Coneybeare.

وولا تحطم بعد الآن، وصورة الخلفية حتى تتحول مثالية (جنبا إلى جنب مع بلدي 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;

و}

و// ارتكاب تحرير، didSelectRow، والذاكرة ... الخ.

وكان المنتدى غير قابلة للملف. م بأكمله دفعة واحدة. من فضلك قل لي إذا كنت تركت شيئا يمكن أن تساعد تشير إلى وجود خطأ.

وفكرت ربما كان ترتيب الطبقات وحاول ذلك من دون أي حظ:

    [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];
    

تأكد من تعيين لون الخلفية من أي UIView كنت تريد أن ترى من خلال ذلك على رأس UINavigationController أو UISplitViewController إلى [UIColor clearColor].

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top