草庐IT

ios - UITableView 在动态单元格内容根据设置更改期间不平滑滚动

coder 2024-01-19 原文

我有一个 TableView ,它根据用户设置显示行数据。 就像我有任务、开始日期、优先级、过程、状态、描述等复选框。

我用户检查任务和描述,然后我创建 UITableViewCell 然后为任务和描述添加标签,如果用户检查所有设置,那么 Cell 应该显示所有创建的标签。

我面临的问题是 UITableView 滚动不流畅,当用户在加载设置后第二次滚动它时,它没有重新使用单元格。

下面是代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellMainNibID = @"cellMain";
    NSUInteger count=[arrSettings count];
    TaskItem* item=[self.arrTaskList objectAtIndex:indexPath.row];
    CGFloat y=2;
    UITableViewCell* cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellMainNibID];
    if (_cellMain == nil)
    {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellMainNibID];


        for (int i=0; i<count; i++)
        {
            UILabel *lbll=(UILabel*)[cell.contentView viewWithTag:indexPath.row+ i+200];
            NSLog(@" cell label = %@",lbll); //**Return null always**
            UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, y, 310, 14)];
            lbl.tag=indexPath.row+ i+200;
            lbl.backgroundColor=[UIColor clearColor];
            lbl.textColor=[UIColor whiteColor];
            lbl.font=[UIFont boldSystemFontOfSize:14];

            [cell.contentView addSubview:lbl];
            lbl=nil;


            NSString* str=[arrSettings objectAtIndex:i];
            CGRect rect;
            if ([str isEqualToString:@"description"])
                rect=CGRectMake(8, y+14, 290, 50);

            else
                rect=CGRectMake(110, y, 290, 14);

            UILabel *lblll=(UILabel*)[cell.contentView viewWithTag:indexPath.row+ i+400];
            NSLog(@" cell label = %@",lblll); //**Return null always**

            UILabel* lbl2=[[UILabel alloc]initWithFrame:rect];
            lbl2.backgroundColor=[UIColor clearColor];
            lbl2.tag=indexPath.row+i+400;
            lbl2.textColor=[UIColor whiteColor];
            lbl2.font=[UIFont systemFontOfSize:14];

            lbl2.numberOfLines=5;
            lbl2.lineBreakMode=NSLineBreakByCharWrapping;
            [cell.contentView addSubview:lbl2];
            y+=lbl2.frame.size.height+1;
            lbl2=nil;

        }
    }

    for (int i=0; i<count; i++)
    {
        UILabel* lbl=(UILabel*)[cell.contentView viewWithTag:indexPath.row+i+200];
        lbl.text=[[[arrSettings objectAtIndex:i]uppercaseString]stringByAppendingString:@":"];

        UILabel* lbl2=(UILabel*)[cell.contentView viewWithTag:indexPath.row+i+400];
        lbl2.text=[item valueForKey:[arrSettings objectAtIndex:i]];
    }






    cell.contentView.backgroundColor=[UIColor clearColor];
    cell.backgroundColor=[UIColor clearColor];
    _cellMain.contentView.backgroundColor=[UIColor clearColor];
    _cellMain.backgroundColor=[UIColor clearColor];

    UIButton *btnCheck=[UIButton buttonWithType:UIButtonTypeCustom];
    btnCheck.tag=indexPath.row+300;
    if ([arrSelectedRow containsObject:[NSNumber numberWithLong:btnCheck.tag-300]])
    {
        [btnCheck setImage:[UIImage imageNamed:@"cb_glossy_on@2x.png"] forState:UIControlStateNormal];
    }
    else
    {
        [btnCheck setImage:[UIImage imageNamed:@"cb_glossy_off@2x.png"] forState:UIControlStateNormal];
    }

    btnCheck.frame=CGRectMake(295, cell.center.y-22, 33, 33);
    [btnCheck addTarget:self action:@selector(checkClicked:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView=btnCheck;
    btnCheck=nil;

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger count=[arrSettings count];
    CGFloat totalHeight=(16*count)+(count*2);
    if (isDesc)
        totalHeight+=60;
    return totalHeight;

}

谁能建议我如何根据 UITableViewCell 标签的用户设置在 UITableView 中显示动态数据?

最佳答案

改变:

if (_cellMain == nil)

if (cell == nil)

关于ios - UITableView 在动态单元格内容根据设置更改期间不平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148096/

有关ios - UITableView 在动态单元格内容根据设置更改期间不平滑滚动的更多相关文章

  1. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  2. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  3. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  6. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  7. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  8. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  9. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  10. ruby - 如何根据长度将路径数组转换为嵌套数组或散列 - 2

    我需要根据字符串路径的长度将字符串路径数组转换为符号、哈希和数组的数组给定以下数组:array=["info","services","about/company","about/history/part1","about/history/part2"]我想生成以下输出,对不同级别进行分组,根据级别的结构混合使用符号和对象。产生以下输出:[:info,:services,about:[:company,history:[:part1,:part2]]]#altsyntax[:info,:services,{:about=>[:company,{:history=>[:part1,:pa

随机推荐