我有一个带有UITableView的UIViewController,我使用界面(拖放标签和UITextViews)在表中绘制了项目,我的表有25行。

现在是时候将标签与IBOutlets链接了,为此,我创建了TableViewCell的子类,如下所示:

TableViewCell.h

@property (nonatomic, retain) IBOutlet UILabel *label1;


TableViewCell.m

@synthesize label1 = _label1;


然后,我使用以下代码更改我的第一个标签:

- (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];

    HobbiesTableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[HobbiesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


    cell.label1.text = @"GG";

    return cell;
}


但是我看到了一个小问题,当我打开模拟器中的View时,打开它的速度太慢而无法打开(我认为我的表正在加载),为了解决此问题,我删除了属性的nonatomic,并且View打开得更快。

但是我有一个问题,当我创建属性时,我总是放命令nonatomic,在这个项目中我不得不将其取消,因为运行缓慢,在该属性中移除nonatomic时会遇到问题吗?标签将永远不会改变!)

最佳答案

您应该将label1设为弱,就像在情节提要中一样。您也不再需要@synthesize

另外,如果要使用不同的标识符出队,则实际上并没有出队。原型单元在情节提要中应具有一个标识符。

NSString *CellIdentifier = @"MyCellID"
 HobbiesTableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


非原子应该可以提高性能,并且不会引起问题。

10-05 21:07
查看更多