【问题标题】:How to create a UITableview cell without using dequeueReusableCellWithIdentifier如何在不使用 dequeueReusableCellWithIdentifier 的情况下创建 UITableview 单元格
【发布时间】:2016-11-12 13:41:35
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Cell *cell = (ChannelViewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];


    NSString *channelName;
    channelName= [NSString stringWithFormat:@"Enter Channel %ld Name", (long)indexPath.row+1];
    indexPathRow= indexPath.row+1;
    return cell;

}

【问题讨论】:

  • 告诉我你为什么要在没有它的情况下进行创作?
  • 你为什么不想使用出队?那就是优化。如果您遇到问题,可能是因为您做错了什么。解释一下,我们可以提供适当的帮助。

标签: objective-c iphone xcode uitableview


【解决方案1】:

您可以使用创建一个

[UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(nullable NSString *)#>]

但正如@MuraliMohan 和@Larme 所说,我们通过避免创建不必要的单元来提高性能。

完成的方式很简单,假设您有 100 个要显示的单元格。由于设备屏幕太小,您一次最多只能向用户显示 15 个单元格,如果不进行出队,您将生成 100 个单元格并显示 15 个。出队的作用是当单元格离开屏幕时,操作系统将将其重用于要显示的下一个单元格,因此您的内存中只有 16 个单元格,而不是 100 个。

基本上,只要继续出队,但如果您想尝试在测试应用中生成大量单元格而不进行出队,看看性能如何下降;)

【讨论】:

  • 谢谢,我知道它会影响性能。但我只需要 6 个单元格。
  • 我相信即使只有 6 个也建议使用双端队列,因为如果没有可用的单元格,操作系统将负责创建单元格,并且最终通过双端队列不会丢失或获得任何东西由你决定使用什么。
  • hmmmmm :) :) :)
【解决方案2】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

    cell.NumberLabel.text =[NSString stringWithFormat:@"Enter Channel %ld Name", (long)indexPath.row+1];
    cell.Name.tag = indexPath.row;

    NSString *dicKey = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

    if (self.cellTextFiledValues[dicKey])
        cell.channelName.text = self.cellTextFiledValues[dicKey];
    else
        cell.channelName.text = @"";

    cell.selectionStyle =NO;
    return cell;
}

- (void)resetTableview{

    self.cellTextFiledValues = [NSMutableDictionary dictionary];
    [self.channelViewTableView reloadData];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    totalChannelCount =[self.datacount.text intValue];
    NSString *dicKey = [NSString stringWithFormat:@"%ld", (long)textField.tag];
    [self.cellTextFiledValues setObject:textField.text forKey:dicKey];
    [self.myTableview reloadData];


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2013-03-26
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多