无法从tableView中选择Cell

时间:2012-12-13 06:14:11

标签: iphone objective-c ios ipad uitableview

嗨朋友,这是我面临的一个奇怪的问题,我在一个分组的TableView中使用多个UItableCustomCells

我只能选择第一节的行,然后当我点击另一节的行时,它的选择方法不起作用,我无法理解错误在哪里,我的cellForRowAtIndexPath代码在下面: -

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


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

    //if (cell == nil) {




    if(indexPath.section==0)
    {
        if(indexPath.row==0)
        {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell1" owner:self options:nil];
            cell = self.Cell1;
            self.Cell1 = nil;

            txtincidentName = (UITextField*)[cell.contentView viewWithTag:1];





        }
        else if(indexPath.row==1)
        {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell2" owner:self options:nil];
            cell = self.Cell2;
            self.Cell2 = nil;
             lblDatefirst = (UILabel*)[cell.contentView viewWithTag:2];
            btnCalfirst  = (UIButton*)[cell.contentView viewWithTag:3];
            lblTimeFirst = (UILabel*)[cell.contentView viewWithTag:4];

        }
        else if(indexPath.row==2)
        {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell2" owner:self options:nil];
            cell = self.Cell2;
            self.Cell2 = nil;

            lblDatefirst = (UILabel*)[cell.contentView viewWithTag:2];
            btnCalfirst  = (UIButton*)[cell.contentView viewWithTag:3];
            lblTimeFirst = (UILabel*)[cell.contentView viewWithTag:4];
            lblInciTime =(UILabel*)[cell.contentView viewWithTag:9];
            lblInciTime.text=@"Date/Time";

        }

    }
    else if(indexPath.section==1)
    {
        if(indexPath.row==0)
        {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell3" owner:self options:nil];
            cell = self.Cell3;
            self.Cell3 = nil;

            btndropDown1 = (UIButton*)[cell.contentView viewWithTag:5];
            btndropDown2  = (UIButton*)[cell.contentView viewWithTag:6];
            btndropDown3 = (UIButton*)[cell.contentView viewWithTag:7];

        }
        else if(indexPath.row==1)
        {
            [[NSBundle mainBundle]loadNibNamed:@"CustomCell4" owner:self options:nil];
            cell = self.Cell4;
            self.Cell4 = nil;
             txtViewofdetails = (UITextView*)[cell.contentView viewWithTag:7];
        }


    }


        return cell;


}

模拟器图片

enter image description here

请帮助和指导谢谢..

2 个答案:

答案 0 :(得分:0)

不要像这样从笔尖加载细胞。而是在NIB中使用重用标识符注册单元。这将导致出队根据标识符选择正确的单元原型。

您当前的方法很可能会弄乱表视图的触摸处理。

答案 1 :(得分:0)

嘿尝试这种类型的概念和代码流

   NSString *CellIdentifier =[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell1 *) currentObject;
                break;
            }
        }
    }