【问题标题】:UITableView changes its style by itselfUITableView 自己改变样式
【发布时间】:2013-07-22 01:43:39
【问题描述】:

我第一次遇到这种问题。

我的 ViewController 上有 UITableView,我在 IB 中选择了 Grouped 样式。 因此,当我在 iPad 上运行我的应用程序时,它是分组样式并且一切正常,但有时 UITableView's 样式变为 Plain。我不会在代码或其他任何地方更改它,它只是自己更改它。

.h

@property (nonatomic, retain) IBOutlet UITableView *myTableView;

.m

myTableView.backgroundColor = [UIColor clearColor];
myTableView.opaque = NO;
myTableView.backgroundView = nil;

我试图删除XIB 并创建一个新的,但它仍然是同样的问题。 有什么想法吗?

UPD

好的,我不知道怎么做,但我在我的项目中有 2 个相同名称的 xib。在一个xib中,我有Plain Style,而在第二个xib中,我有Grouped style;所以它解释了为什么有时我有 Grouped 和somteimes Plain Style。我刚刚删除了其中一个,它解决了问题。

【问题讨论】:

  • 那么委托方法呢?请包括数据源方法。而且它从未发生在我身上。请确保在代码中没有指定样式以编程方式更改的地方

标签: objective-c cocoa-touch uitableview xib


【解决方案1】:

UITableView 样式不会自动更改,除非您在 xib 或代码中更改样式。请仔细检查您的代码,看看您是否正在更改样式,并确保您已正确连接数据源和委托。
听到是另一种可以在代码中而不是在 xib 中创建的方法。 在下面的代码中创建表格视图给了你一些想法。


 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

 {

 UITableView *aTableVIew;
 }

@end

  @implementation ViewController


 - (void)viewDidLoad
{

  [super viewDidLoad];
  //Do any additional setup after loading the view, typically from a nib.

aTableVIew = [[UITableView alloc]initWithFrame:self.view.bounds  style:UITableViewStyleGrouped];

aTableVIew.dataSource = self;
aTableVIew.delegate = self;
[self.view addSubview:aTableVIew];
}


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


  -(void)dealloc
  {
       [aTableVIew release];
       [super dealloc];
  } 

 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

   return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   return 2;
}

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{

    UITableViewCell *cell = [aTableVIew dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil)
{

      cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"cell"]autorelease];
  }
  if(indexPath.section == 0)
  {
     cell.textLabel.text = @"Hello";
  }
  else if (indexPath.section == 1)
  {
    cell.textLabel.text = @"World";
  }
  else
  {
     cell.textLabel.text = @"Happy coding";
  }
  return cell;
 }

 @end


【讨论】:

  • 谢谢你,Shaun,我认为以编程方式创建 UITableView 是我目前最好的选择。但我就是不明白,为什么有时它是分组的,在第二次“构建并运行”之后它变成了普通的。在我再次构建并运行该项目之后,它再次分组。依此类推 - 分组、普通、分组、普通等。
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 2018-07-07
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
相关资源
最近更新 更多