我正在尝试在 UIViewController 上创建一个悬停的 UITableViewController。我已经设法通过 ViewController 打开 TableView。(图片)
首次加载视图时,数据已加载,我可以看到一瞬间的行,但随后它消失了。我知道我必须在内存中的实际视图上分配一些东西。但是我无法找出我做错了什么。
这是我尝试这样做的方法:
我将它创建为一个对象并用动画加载它
BINotificationView *notifView = [[BINotificationView alloc] init];
notifView.view.frame = CGRectMake(10, 10, 300, 400);
[notifView.tableView setDataSource:notifView];
[notifView.tableView setDelegate:notifView];
[notifView setResultSet:_notifSet];
notifView.view.layer.masksToBounds = YES;
[self.view addSubview:notifView.view];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[notifView.view setAlpha:1.0];
[UIView commitAnimations];
[UIView setAnimationDuration:0.0];
加载视图时,会填充表格视图(情节提要中具有相同的单元格标识符)
@interface BINotificationView ()
@end
@implementation BINotificationView
@synthesize resultSet;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.view.alpha = 0.0;
self.view.layer.cornerRadius = 5;
self.view.layer.borderWidth = 3.0f;
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (resultSet.count) {
return resultSet.count;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"notif";
NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[NotificationCell alloc] init];
cell.frame = CGRectMake(0, 0, 300, 80);
}
cell.notification = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 80)];
[cell addSubview:cell.notification];
[cell.notification setFont:[UIFont fontWithName:@"AvenirNextCondensed-Regular" size:16]];
if (resultSet.count) {
BINotification *obj = [resultSet objectAtIndex:indexPath.row];
cell.notification.text = obj.type;
}
else {
cell.notification.text = @"Empty List";
}
[cell.notification setEditable:NO];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
我的 UITableViewCell 定义为
@interface NotificationCell : UITableViewCell
@property (nonatomic, retain) IBOutlet UITextView *notification;
@end
@implementation NotificationCell
@synthesize notification;
@end
UITableViewController .h 文件是这样的
@interface BINotificationView : UITableViewController
@property (nonatomic, strong) NSMutableArray *resultSet;
@end
谢谢你的帮助