How to use different cell in one table view?

ぐ巨炮叔叔 提交于 2019-12-25 03:48:07

问题


How to use 3 different cells on one uitableview? I have 3 buttons now When i press any button then cell of table view should be changed. Please help


回答1:


In the button's action method, save which button was tapped & reload the row in which you want to change the cell.

-(void)button1Tapped:(id)sender
{

    self.buttonIndex = 1;
    NSArray* arr = [[NSArray alloc] initWithObjects:self.indexPathOfConcernedCell, nil];
    [self.tableView reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationNone];
    [arr release];

}

Then, in cellForRowAtIndexPath: return the cell one the basis of which button was pressed-

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

    if(indexPath == self.indexPathOfConcernedCell)
    {

    if(self.buttonIndex == 1)
             {
                  Cell1* cell = (Cell1*) [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier1"];

                  if(cell == nil)
                  {
                 cell = [[[Cell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier1"] autorelease];
                  } 
             }

        else if(self.buttonIndex == 2)
        {
            //do similar stuff
        }

        //do similar stuff for buttonIndex 3 here

        }
    }

    else
    {
        //your regular stuff
    }

    return cell;

    }


来源:https://stackoverflow.com/questions/7107157/how-to-use-different-cell-in-one-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!