ios - 我以编程方式创建了一个 TableView ,如下所示 -

标签 ios objective-c uitableview

- (void)viewDidLoad {
    [super viewDidLoad];
    menuArray=[[NSMutableArray alloc]initWithObjects:@"Address",@"Restaurants",@"Deals",@"Orders",@"Account",@"Address Book",@"Settings",@"Live Chat",@"Info",nil];
    tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, menuSubView.frame.size.height, self.view.frame.size.width-80, self.view.frame.size.height-menuSubView.frame.size.height)];
    tableView.delegate=self;
    tableView.dataSource=self;
    [self.view addSubview:tableView];
}

和其他方法如下-

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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reusableIdentifier=@"Cell4";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableIdentifier];
    }
    cell.textLabel.text=[menuArray objectAtIndex:indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

然而,当我运行程序时,我能够看到所需的表格 View 。但是当我滚动浏览它时,表格的数据消失了,只剩下空白的表格 View 。谁能帮我?提前致谢。

最佳答案

--> 我感谢以上回答。

--> 请在“cellForRowAtIndexPath”方法中尝试这一行...!

--> 有时会发生。我过去用这条线解决了这个问题。我希望它对你有用。

UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:@"Cell4" forIndexPath:indexPath];

关于ios - 我以编程方式创建了一个 TableView ,如下所示 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401735/

相关文章:

ios - 像这样子类化 NSDictionary : @implementation NSDictionary (weather)

objective-c - 以编程方式访问 AWS IAM 用户策略和访问权限

objective-c - 根据内容长度修改多行 NSTextField 的字体大小

ios - menuController Action 的 tableView 设置编辑不起作用

ios - NSPredicate SELF CONTAINS[c] 首先返回较长文本中包含的值

ios - 我应该如何从合适的选择中保存用户设置?

ios - 免费应用内购买

ios - 使用 Property List/NSMutableDictionary 的内存泄漏

ios - 如何阻止 Xcode 忽略我的 iOS 应用程序的 CoreLocation.framework?

ios - 如何从连续的 URL 中获取数据