如何在运行时将一行插入iPhone上的UITableView?

时间:2012-01-31 06:26:11

标签: iphone objective-c ios uitableview

我创建了一个按钮,其中包含在运行时向UITableView添加行的操作,但我的代码无效。我可以看到动画向下的行,但是仅在UITableView行上显示的动画未添加。我可以在此代码中查看在单元格中添加的行?

我有4个部分,我想在第0行的第1部分和第0行的第2部分添加行:

-(IBAction)add:(id)sender
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray* path = [NSArray arrayWithObject:indexPath];

    // fill paths of insertion rows here
    [self.mytableview beginUpdates];
    [self.mytableview insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];      
    [self.mytableview deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
    [self.mytableview endUpdates];
    [self.mytableview reloadData];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger rows;
    if (section==0) {
        rows = 4;
        //return    rowForSectionOne;
        //rows=rowForSectionOne++;
    }
    if (section == 1) 
    {
        rows = 1;
    }

    return rows;        
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([indexPath row] == 0 && [indexPath section] == 0)
    {
        cell.textLabel.text=@"Title";

        cell.accessoryView = textField;
        titlename=textField.text;
        [[cell imageView] setImage:[UIImage imageNamed:@"DetailViewDue.png"]];
        NSLog(@"******:%@",titlename);          
    }

    if ([indexPath row] == 1 && [indexPath section] == 0)
    {
        cell.textLabel.text=@"Tags";
        cell.detailTextLabel.text=app.Tags;
     [[cell imageView] setImage:[UIImage imageNamed:@"DetailViewTag.png"]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if ([indexPath row] == 2 && [indexPath section] == 0)
    {
        cell.textLabel.text=@"Notes";
        cell.detailTextLabel.text=app.Notes;
        [[cell imageView] setImage:[UIImage imageNamed:@"DetailViewNote.png"]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    fromDate = [[dateFormat stringFromDate:selectionData.fromDateSelected]retain];

    if ([indexPath row] == 3 && [indexPath section] == 0)
    {
        cell.textLabel.text=@"DueDate";
        cell.detailTextLabel.text=fromDate;
        [[cell imageView] setImage:[UIImage imageNamed:@"DetailViewDue.png"]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    if ([indexPath row] == 0 && [indexPath section] == 1)
    {
        cell.textLabel.text=@"Attach";

    }

    return cell;
}

2 个答案:

答案 0 :(得分:1)

你做得对。让我们说按下按钮时会调用-(IBAction)add:(id)sender。然后使用正确的indexPath&撰写rowsection。这里的部分是0,1,2,3(因为你有4个部分 -

NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:0 inSection:2];
NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:0 inSection:3];    
//put these indexpaths in a NSArray

[tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];

这应该更新表格。由于您只添加一行(并且不更改整个表格),因此无需在表格上执行reloadData。另外,请确保dataSource每个部分都添加了此新增条目,否则您的应用将崩溃

答案 1 :(得分:1)

您只需在“cellForRowAtIndexPath”方法中添加几行代码

   if(cell ==nil)
      {
        cell =[[UITableViewAlloc alloc]initWithStyle.... ]
      }
   int theRow = indexPath.row;
   if(indexPath.section  == 1) theRow += 3;
   if(indexPath.section  == 2) theRow += 5;
   if(indexPath.section  == 3) theRow += 4;
   if(indexPath.section  == 4) theRow += 3;

  //load the view in it

  cell.textLable . text = [<your object> objectAtIndexPath.row];
 return cell;

  Here you can add rows as many as you want....