在UItableview中添加一行

时间:2012-08-10 10:54:10

标签: iphone ios uitableview

我无法在UITableview中添加新行,代码如下, 它显示错误

-(void)viewDidLoad

{    
    self.title=@"CHECK-list";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)]; 
    self.navigationItem.leftBarButtonItem = addButton;

    ar=[[NSMutableArray alloc]initWithObjects:@"Map",@"Camera",@"First Aid Kit", nil];    
    [super viewDidLoad];

}

- (void)addItem
   {
   //NSMutableArray *tempArray = [[NSMutableArray alloc] init];
   int i = 0;
   for (NSArray *count in ar)
   {
    [ar addObject:[NSIndexPath indexPathForRow:i++ inSection:1]];
   }

    [[self tableView] beginUpdates];
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)ar withRowAnimation:UITableViewRowAnimationNone];
    [[self tableView] endUpdates];
    [self.tableView reloadData];   
  }

1 个答案:

答案 0 :(得分:1)

这里是Fazil ....任何tableview和行添加的基本代码

  - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

  UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(160, 280, 150, 150) style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;

 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier ];
    } 
    cell.textLabel.text= [array objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_normal.png"]];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
    return cell;
}

#pragma mark  UItableView delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    check = indexPath.row;
    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
    theCell.contentView.backgroundColor=[UIColor clearColor];
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
}

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
[tableview moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:[array count] - 1 inSection:1]];
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
        NSInteger row = 0;
        if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
            row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
        }
        return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];     
    }
    return proposedDestinationIndexPath;
}