简体   繁体   English

如何使用故事板中没有可重用功能的UITableView上的xib文件使用TableViewCell

[英]How to use TableViewCell from xib file on UITableView with no Reusable feature in storyboard

I have this UITableViewCell from a .xib file 我有一个来自.xib文件的UITableViewCell 在此处输入图片说明

And I'm using it on a UITableViewCell inside of a storyboard with the following code: 我正在故事板内部的UITableViewCell上使用以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [requestTableView registerNib:[UINib nibWithNibName:@"RequestAcceptTableViewCell" bundle:nil] forCellReuseIdentifier:@"RequestAcceptCellIdentifier"];
    requestTableView.estimatedRowHeight=168;
    requestTableView.rowHeight=UITableViewAutomaticDimension;
    [requestTableView reloadData];
    requestTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

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


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

    return [requestArray count];
}

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

    RequestAcceptTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RequestAcceptCellIdentifier"];
    if (cell == nil) {
        cell = [[RequestAcceptTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                 reuseIdentifier:@"RequestAcceptCellIdentifier"];
    }

    return cell;
}

so far this works good: 到目前为止,这很好用: 在此处输入图片说明

But I want this to stop reusing UITableViewCells everytime I scroll on my UITableView since I just want to have there a maximum of 5 UITableViewCells inside, I tried with: 但是我希望每次滚动UITableView时都不要再使用UITableViewCells,因为我只想在其中最多容纳5个UITableViewCells,所以我尝试了:

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

    RequestAcceptTableViewCell = [[RequestAcceptTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                                     reuseIdentifier:nil];
    return cell;
}

and then I get this: 然后我得到这个:

在此处输入图片说明

My UITableView has a UITableViewCell with no format, buttons, labels... is there a way to call a UITableViewCell by it's identifier but without reusing it?? 我的UITableView有一个没有格式,按钮,标签的UITableViewCell ...有没有一种方法可以通过其标识符来调用UITableViewCell而不重用它? I mean, keeping it in memory... 我的意思是,将其保存在内存中...

thanks for the support 感谢您的支持

If you do not wish to reuse UITableViewCell, you have to: 如果您不想重复使用UITableViewCell,则必须:

  • Use Static Cells instead of Dynamic Prototypes . 使用Static Cells而不是Dynamic Prototypes
  • Set number of rows to 5 on: storyboard -> Table View -> Table View Section -> Rows : 5. 在以下页面上将行数设置为5:情节提要->表格视图->表格视图部分->行数:5。
  • Remove all data source methods ( numberOfRowsInSection & cellForRowAtIndexPath ) -> IMPORTANT . 删除所有数据源方法( numberOfRowsInSectioncellForRowAtIndexPath )-> 重要
  • Drag and drop IBOutlets from storyboard to your implementation file. IBOutlets从情节IBOutlets板拖放到实现文件中。
  • Then you can configure your labels/buttons/views from their referenced IBOutlet. 然后,您可以从参考的IBOutlet中配置标签/按钮/视图。

Update: 更新:

If you wish to have the Dynamic Prototypes you could do this: 如果您希望拥有Dynamic Prototypes ,可以这样做:

Remove this line: RequestAcceptTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RequestAcceptCellIdentifier"]; 删除以下行:RequestAcceptTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@“ RequestAcceptCellIdentifier”];

Add this: 添加:

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

    RequestAcceptTableViewCell *cell = [[RequestAcceptTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM