static NSString *ID的改进

时间:2023-03-09 07:18:52
static NSString *ID的改进

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *ID = @"testcell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

cell.textLabel.text = @"test";

return cell;

}

上面这段代码你可能已经,行云流水于指尖。

其实下面这样写也是对的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testcell" forIndexPath:indexPath];

cell.textLabel.text = @"test";

return cell;

}

OC中如果直接这样定义字符串 @"testcell"  ,那么这个字符串会被放到常量区,所以 不存在反复创建的说法,这也是后来xcode版本中的改变,更早的xcode是不支持这种操作的。