以编程方式添加UITableView - 如何设置单元格的重用标识符?

Dou*_*ith 6 iphone objective-c uitableview uiviewcontroller ios

我有一个UIViewController,在某些时候增长了一个UITableView,当它发生时,我只是初始化TableView实例变量并将其添加到视图中,但我不知道如何处理要添加到视图中的单元格的出列; 我需要一个重用标识符,但我不确定如何设置它.

我该怎么办?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"wot";

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

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

MJN*_*MJN 7

使用该方法 initWithStyle:reuseIdentifier

  1. 检查是否cell存在
  2. 如果没有,那么您需要初始化它.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath 
{
    static NSString *cellIdentifier = @"wot";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)