Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法设置UILabel属性_Iphone_Objective C_Ios - Fatal编程技术网

Iphone 无法设置UILabel属性

Iphone 无法设置UILabel属性,iphone,objective-c,ios,Iphone,Objective C,Ios,我无法设置UILabel对象的属性 以下代码位于-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath方法中: UILabel *label = (UILabel *)[cell viewWithTag:0]; label.text = [self.comments objectAtIndex:indexPath.row*2]; label.textColor

我无法设置UILabel对象的属性

以下代码位于
-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
方法中:

UILabel *label = (UILabel *)[cell viewWithTag:0];
label.text = [self.comments objectAtIndex:indexPath.row*2];
label.textColor = [UIColor cyanColor];
label.adjustsFontSizeToFitWidth = YES;
我得到的只是第四行的一个运行时错误(奇怪的是,前几行还可以):

类似的标签属性修改,例如
label.numberOfLines=1
label.minimumFontSize=7.0使程序也崩溃


这里有什么我不明白的微妙之处吗?

您需要使用cell.textLabel来访问label,现在,由于“label”本身引用了UITableViewCell,它给出了错误。

您正在使用不同的对象制作标签(标记0表示单元格,而不是表示标签)。检查为UILabel制作的对象, 它不是UILabel类型。它采用UITableViewCell类型


这就是它导致崩溃的原因。

如果要访问内置于标准UITableViewCell中的标签,请使用单元格的textLabel属性:

UILabel *label = cell.textLabel;
...
如果将自定义标签添加到单元格中,我建议使用一些非零标记,因为0是默认标记值,所以

[cell viewWithTag:0];
可以返回任何带有0标签的子视图-不需要您的标签。因此,为标签指定一些非零标记,并将其添加到单元格的contentView中,而不是直接添加到单元格-这将帮助您避免一些布局问题,例如当单元格进入编辑状态时。稍后,您可以通过以下方式访问该标签:

UILabel *label = (UILabel*)[cell.contentView viewWithTag:kSomeNonZeroTag];

text和textColor方法仍然有效的原因是UITableViewCell实际实现了这些方法(它们起源于cell没有公开其标签的时代,并且自SDK 3.0以来一直被弃用)

实际上
[cell viewWithTag:0]
返回
UITableViewCell
本身。因此,只需将UILabel的
标记更改为其他标记,如100、101等,

您可以按照以下方式进行操作:

[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];

[cell.textLabel setTextColor:[UIColor colorWithRed:222.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0]];

[cell.textLabel setBackgroundColor:[UIColor clearColor]];
您可以这样调整任何属性

如果有任何困难,请告诉我


干杯。

为您的子视图添加适当的标签,然后再试一次。或者,如果要设置表视图单元格的默认标签,则应在TableViewCell类的标题中使用cell.textLable

@属性(非原子,保留)UILabel*textLabel

在TableViewCell实现类中,确保您有
@synthesis textlab

在CellForRowatinex中,您可以从TableViewCell类创建单元格。你可以用这个函数得到细胞

MyCustomizedCell*单元格=(MyCustomizedCell*)[tableView dequeueReusableCellWithIdentifier:(NSString*)myIndent]

依此类推,您可以尽可能轻松地使用TableViewCell中的每个对象


cell.textlab.adjustsFontSizeToFitWidth=是;


[cell.textlab setAdjustsFontSizeToFitWidth:是];

看起来
viewWithTag:
正在返回一个
UITableViewCell
对象。前面几行是可以的,因为
text
textColor
也是
UITableViewCell
的属性(不推荐使用)。他们不谈论那个问题。是否有方法从UIView获取UILabel,UIView由
viewWithTag:
返回?所有视图的默认值都是0标记,因此您可能需要使用不同的值。
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];

[cell.textLabel setTextColor:[UIColor colorWithRed:222.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0]];

[cell.textLabel setBackgroundColor:[UIColor clearColor]];