且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

初始化自定义UITableViewCell

更新时间:2022-11-16 07:54:46

好的。这不是UITableView的工作方式。当表视图需要绘制一个单元格(即一行)时;它会在 dataSource 属性中指定的对象上调用 tableView:cellForRowAtIndexPath:。从该方法返回UITableViewCell是您的工作。这就是Apple的做法(以及应该如何做):

Okay. That's not how UITableView works. When the table view needs to draw a cell (ie, a row); it invokes tableView:cellForRowAtIndexPath: on the object specified in the dataSource property. It's your job to return a UITableViewCell from that method. This is how Apple does it (and how you should do it):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnIdentifierString"];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
    }

    cell.textLabel.text = @"This text will appear in the cell";


    return cell;
}

该方法的调用次数取决于其中的节数表视图和每个节中的行数。该过程如下:

The number of times that method will be invoked depends on the number of sections in the table view and the number of rows in each section. The process works like this:


  1. 表视图调用委托方法 numberOfSectionsInTableView: dataSource (它知道它实现了该方法,因为 dataSource 必须遵守 UITableViewDataSource 协议。)

  2. 如果 numberOfSectionsInTableView:返回大于零的数字,则表视图将调用委托数据源上的方法 tableView:numberOfRowsInSection:。因此,如果 numberOfSectionsInTableView:返回 2 ,则 tableView:numberOfRowsInSection:

  3. 如果每次调用 tableView:numberOfRowsInSection:都返回一个大于零的数字,则表视图将调用在 dataSource 上使用委托方法 tableView:cellForRowAtIndexPath:'因此,如果 tableView:numberOfRowsInSection:返回 5 tableView:numberOfRowsInSection:将被调用五次(每行一次) 。

  4. 自定义该单元格的显示方式的机会是在您收到可用的单元格之后,但在返回之前(其中此文本将显示在单元格显示在上方)。您可以在这里做很多事情;您应该会看到UITableViewCell的类参考,以查看您可以做的所有事情(我所做的全部都将其设置为显示此文本...)。 上方行是iOS出于性能考虑重新使用单元的一种方式。例如,如果要显示字符串数组中的某个字符串,则可以执行此操作(注意使用 indexPath 变量): cell.textLabel.text = [someArrayYouHave objectAtIndex:indexPath.row];

  1. Table View invokes the delegate method numberOfSectionsInTableView: on its dataSource (it knows it implements that method because the dataSource must adhere to the UITableViewDataSource protocol).
  2. If numberOfSectionsInTableView: returns a number greater than zero, the table view will invoke the delegate method tableView:numberOfRowsInSection: on the dataSource. So if numberOfSectionsInTableView: returns 2, tableView:numberOfRowsInSection: will be invoked twice.
  3. If each invocation of tableView:numberOfRowsInSection: returns a number greater than zero, the table view will invoke the delegate method tableView:cellForRowAtIndexPath: on the dataSource' So if tableView:numberOfRowsInSection: returns 5, tableView:numberOfRowsInSection: will be invoked five times (once for each individual row).
  4. Your opportunity to customise how that cell appears is after you've received a useable cell, but before it is returned (where 'This text will appear in the cell' appears above). You can do quite a lot here; you should see the Class Reference for UITableViewCell to see everything you can do (all I've done is set it to show 'This text...'). The lines above that are a way for iOS to reuse cells for performance considerations. If you, for example, wanted to show a certain string from an array of strings, you could do this (notice the use of the indexPath variable): cell.textLabel.text = [someArrayYouHave objectAtIndex:indexPath.row];.