在UILabel上使用setText时,无效的Argument异常

问题描述

| 我疯了尝试调试问题,但出现异常
*** Terminating app due to uncaught exception \'NSinvalidargumentexception\',reason: \'-[NSCFString setText:]: unrecognized selector sent to instance 0x5799c90\'
抛出的代码是:
- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{

    for(int index=0;index<[pendingDownloadsArray count];index++)
    {
        if ([request isEqual:[[pendingDownloadsArray objectAtIndex:index]objectForKey:@\"request\"]])
        {
            NSNumber *x = [[pendingDownloadsArray objectAtIndex:index] objectForKey:@\"completed\"];

            float newCompleted = [x floatValue]+(float)bytes;
//          x+=(float)bytes;
                        NSLog(@\"Completed: %fKb\",newCompleted/1024);
            x = [NSNumber numberWithFloat:newCompleted];

            [[pendingDownloadsArray objectAtIndex:index] setobject:x forKey:@\"completed\"];
            float progress = newCompleted / (float)request.contentLength;

            //Dont try to get cell if the table is showing something else.
            if(self.selectedSegment ==0)
            {
                DownloadsCustomCell *cell =(DownloadsCustomCell*) [downloadsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
                [cell.downloadProgressView setProgress:progress];
                Nsstring *progressLabelText = [Nsstring stringWithFormat:@\"%.2f percent (%.2fKb / %.2fKb)\",progress*100,newCompleted/1024,((float)request.contentLength/1024)];
                NSLog(@\"%@\",progressLabelText);
                UILabel *label = cell.downloadProgressLabel;
                label.text = progressLabelText;
            }

        }

    }
}
在最后一行抛出异常。 (起初我没有不必要的\'label \'...所以它的存在标志着我多么困惑。 要么是我不知道的问题,要么是我做一些我无法发现的愚蠢的事情。 您能帮我将Nsstring分配给该UILabel吗? (PS:我检查了一下,下载ProgressLabel是一个UILABEL。)     

解决方法

选项1-问题是标签已被释放。 cell.downloadProgressLabel指向错误的内存,有时碰巧是一个字符串,执行该代码时应该遇到不同的错误。 选项2-您正在将downloadProgressLabel创建为字符串而不是uilabel,或者正在执行cell.downloadProgressLabel = [某些字符串对象];错误地在代码中的某处,尽管应该发出警告:)     ,我对下面的行有疑问,请检查
downloadProgressLabel
的类型,它可能是在您的单元格类中声明的
NSString
, 使用下面的代码。
if ([cell.downloadProgressLabel isKindOfClass:[UILabel class]])
{
    UILabel *label = cell.downloadProgressLabel;
    label.text = progressLabelText;
}
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...