UITableViewCell使用

徐绪
2023-12-01

From App Doc

背景:UITableView上多个UITextField控件

现象:当连续点击不在同一个显示界面的UITextField时,崩溃

提示:多个UITextField setFirstResponder

代码:

    UITableViewCell *cell =
        cell = nil;
cell=[[UITableViewCell alloc] init];

调整如下,崩溃消失


        DetailTableViewCell *cell = (DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier: OrdinaryDeatilTableViewCellIdentifier];
        cell = nil;
        if (cell == nil) {
            cell = [[DetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: OrdinaryDeatilTableViewCellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }


原因分析:如果采用重用机制,内部对响应链做了处理。

initWithStyle:reuseIdentifier:

Initializes a table cell with a style and a reuse identifier and returns it to the caller.

- (id)initWithStyle:( UITableViewCellStyle) style reuseIdentifier:( NSString *) reuseIdentifier
Parameters
style

A constant indicating a cell style. See “UITableViewCellStyle” for descriptions of these constants.

reuseIdentifier

A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view. Pass nil if the cell object is not to be reused. You should use the same reuse identifier for all cells of the same form.

Return Value

An initialized UITableViewCell object or nil if the object could not be created.

Discussion

This method is the designated initializer for the class. The reuse identifier is associated with those cells (rows) of a table view that have the same general configuration, minus cell content. In its implementation of tableView:cellForRowAtIndexPath:, the table view'€™s data source calls the UITableView methoddequeueReusableCellWithIdentifier:, passing in a reuse identifier, to obtain the cell object to use as the basis for the current row.

If you want a table cell that has a configuration different that those defined by UITableViewCell for style, you must create your own custom cell. If you want to set the row height of cells on an individual basis, implement the delegate method tableView:heightForRowAtIndexPath:.


 类似资料: