【发布时间】: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