开发者

dequeueReusableCellWithIdentifier: always returns 'nil' for visible cells

开发者 https://www.devze.com 2023-04-09 05:22 出处:网络
I create a custom table view cell. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

I create a custom table view cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITextField *editField=nil;
...

NSString *CellIdentifier = [NSString  stringWithFormat:@"cell:%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Configure the cell...
    switch (indexPath.row) {

        case 0: 
        {

            cell.textLabel.text=DEVNAME_TEXT_NDVC;
            cell.textLabel.font=[UIFont boldSystemFontOfSize:LABEL_TEXTSIZE_NDVC];

            editField=[[UITextField alloc] initWithFrame:CGRectMake(158, 9, cell.frame.size.width-183, cell.frame.size.height-15) ];
            editField.tag=DEVNAME_TAG_NDVC;
            ...
            [cell.contentView addSubvie开发者_运维技巧w:editField ];
            [editField release];

        }
        break;

The table has 5 lines only, and each of them is on the screen always. Later, when I try to get access to the cell I always get 'nil'

The following code should place cursor to apropriate UITextField when user tap the cell, but it doesn't, since 'cell' is always =0.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *CellIdentifier = [NSString stringWithFormat:@"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *tf=nil;

[tableView deselectRowAtIndexPath: indexPath animated: YES];
[activeField resignFirstResponder]; // Last used UITextField

switch (indexPath.row) {
    case 0: //
        tf=(UITextField*)[cell.contentView viewWithTag:DEVNAME_TAG_NDVC];
        [tf becomeFirstResponder]; // Show the keyboard
        //[tf performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.7];
    break;

Please, could you suggest what is wrong? Why [tableView dequeueReusableCellWithIdentifier:CellIdentifier] always =0, but all of the table cells are always visible.

Thanks.


Maybe I don't understand the question, but don't table cells only become reusable once they are no longer being displayed? If they are still visible, how could you reuse them?


Change this:

NSString *CellIdentifier = [NSString  stringWithFormat:@"cell:%d",indexPath.row];

to:

static NSString *CellIdentifier = @“XXXX”;


Yes, dequeueReusableCellWithIdentifier: always return nil EXCEPT using registerNib:forCellReuseIdentifier:.

0

精彩评论

暂无评论...
验证码 换一张
取 消