【问题标题】:Data not shown in UITableView below button按钮下方的 UITableView 中未显示数据
【发布时间】:2011-04-15 11:18:48
【问题描述】:

我为 Iphone 创建了 UI,在 tableview 上方有一个按钮和标签。问题是尽管在 cellForRowAtIndexPath 方法中设置了数据,但数据并未显示在表格中。 我明白了:

这是我的第三个标签控制器代码。标题:

#import <UIKit/UIKit.h>

   @interface ThirdView : UIViewController <UITableViewDelegate,UITableViewDataSource> {
//model
NSMutableArray *podatki;
//view
 UITableView *myTableView;
 }

@property(nonatomic,retain) NSMutableArray *podatki;
@property(nonatomic,retain) UITableView *myTableView;

-(IBAction)pritisnuGumb:(UIButton *) sender; //

@end

实施:

#import "ThirdView.h"

@implementation ThirdView

  @synthesize podatki;
  @synthesize myTableView;

   -(void)viewDidLoad{

myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]    style:UITableViewStylePlain]; 
myTableView.delegate = self; 
myTableView.dataSource = self; 

myTableView.autoresizesSubviews = YES; 

podatki = [[NSMutableArray alloc] init];    

[podatki addObject:@"Sunday"];
[podatki addObject:@"MonDay"];
[podatki addObject:@"TuesDay"]; 

[super viewDidLoad];
//self.view = myTableView;

 }

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

 -(IBAction)pritisnuGumb:(UIButton *) sender {

  NSLog(@"buca");
 }

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];   
}

cell.textLabel.numberOfLines = 4; 
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

NSString *naslov = [podatki objectAtIndex:indexPath.row];
cell.textLabel.text = naslov;    

return cell;
   }

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"kliknu na vrstico");

   }

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

  - (void)didReceiveMemoryWarning
  {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
 }

 #pragma mark - View lifecycle



  - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
 }

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

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

 @end

如果我取消注释行“self.view = myTableView;”我得到带有数据的表格视图,但它上面的标签和按钮消失了(表格视图是全屏的)。

我在这里做错了什么?

@詹尼斯: 我尝试了您的解决方案,现在可以看到表格内的数据,但上部像这样被挤压:

【问题讨论】:

  • 现在你只需要通过给出 [myTableView setFrame:CGRectMake(, , , )]。只需根据您的要求为 x y 和宽度高度提供适当的值。我猜它现在应该可以工作了。如果这解决了您的问题,请接受答案。
  • 我必须像你说的那样设置 [myTableView setFrame:CGRectMake ......] 并删除 [self.view sendSubviewToBack:myTableView] 以使其工作。谢谢!

标签: iphone uitableview button


【解决方案1】:

相信你也能做到

[self.view addSubview:myTableView];

[self.view sendSubviewToBack:myTableView]。

希望对你有帮助

【讨论】:

  • 我更新了我的问题以显示当我添加你的行时会发生什么。
猜你喜欢
  • 2018-10-05
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2020-08-22
相关资源
最近更新 更多