gpt4 book ai didi

ios - 主从应用程序背景

转载 作者:行者123 更新时间:2023-11-29 13:16:21 24 4
gpt4 key购买 nike

我的主从应用程序背景有问题。当我启动应用程序而不是显示我设置的背景图像时,它会显示以下内容: enter image description here

当我按下添加按钮时,它变成了真实的背景: enter image description here

这是我正在使用的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
}

return self;
}

- (void)viewDidLoad
{

[super viewDidLoad];

bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];


// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.backgroundView = bgView;
static NSString *CellIdentifier = @"Cell";
tableView.separatorColor = [UIColor colorWithHue:0 saturation:0 brightness:33 alpha:0.2];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


}

cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cell.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];

NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
}
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}

如何让它直接显示背景图片?谢谢。

最佳答案

不要创建另一个 View ,只需将其放入 viewDidLoad

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];
self.tableView.backgroundView = nil;

并删除这些

//in viewDidLoad
bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];
//in cellForRowAtIndexPath:
tableView.backgroundView = bgView;

或尝试放置

tableView.backgroundView = bgView;

viewDidLoad 中,它应该可以工作。

您遇到此问题的原因是因为您在设置单元格时调用的方法中设置了 tableView 的 backgroundView,但是因为表格 View 中还没有单元格,在 View 中至少有一个单元格之前,不会调用该方法。

关于ios - 主从应用程序背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15709910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com