需要改进这种方法

时间:2014-02-04 10:30:12

标签: oop switch-statement xcode5

这里是场景,如果有人知道,我想在此代码中使用switch方法,将不胜感激 没有一个很大的逻辑,但有人也可以描述它在switch语句中的工作方式

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"cell";

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


        if (indexPath.section == 0)

        {

        Preface *preface_access = [preface objectAtIndex:indexPath.row];
        cell.textLabel.text = preface_access.preface_title;



        }

        else if (indexPath.section == 1)

        {
            Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
            cell.textLabel.text = chapter_access.chapter_title;

        }


        else
        {
            Index * index_access = [index objectAtIndex:indexPath.row];
            cell.textLabel.text = index_access.index_title;    
        }

        return cell;
    }

提前致谢



我试过这个而不是上面的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

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


    switch (indexPath.section) {
    case 0:
        {

            Preface *preface_access = [preface objectAtIndex:indexPath.row];
            cell.textLabel.text = preface_access.preface_title;
            break;
        }
    case 1:
        {
            Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
            cell.textLabel.text = chapter_access.chapter_title;
        }

    case 2:
   {
        Index * index_access = [index objectAtIndex:indexPath.row];
        cell.textLabel.text = index_access.index_title;
   }


    return cell;
}


错误来自此方法预期';'在表达之后


**tableview ;**

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

2 个答案:

答案 0 :(得分:1)

请尝试此片段(虽然未经测试):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    switch (indexPath.section)
    {
        case 0:{
            Preface *preface_access = [preface objectAtIndex:indexPath.row];
            cell.textLabel.text = preface_access.preface_title;
            return cell;
        }
        case 1:{
            Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
            cell.textLabel.text = chapter_access.chapter_title;
            return cell;
        }
        case 2:{
            Index * index_access = [index objectAtIndex:indexPath.row];
            cell.textLabel.text = index_access.index_title;
            return cell;
        }
        default:
            return cell;
    }
}

答案 1 :(得分:0)

sel=indexPath.section;
switch(sel)

{
    case 0:
      Preface *preface_access = [preface objectAtIndex:indexPath.row];
      cell.textLabel.text = preface_access.preface_title;
    break;
    case 1:
      Chapters * chapter_access = [chapters objectAtIndex:indexPath.row];
      cell.textLabel.text = chapter_access.chapter_title;
    break;
    default:
      Index * index_access = [index objectAtIndex:indexPath.row];
      cell.textLabel.text = index_access.index_title;    
    break;
}

你是说这个吗?