2012-08-10 31 views
0
添加一行

我不能够在UITableview添加新行,代码如下, 它显示与在UITableView的

-(void)viewDidLoad 

{  
    [email protected]"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]; 
    } 
+0

你来自哪个方法? – IronManGill 2012-08-10 11:01:12

+0

我在class1.m文件中创建了这段代码。 – fazil 2012-08-10 11:02:52

+3

我不是很确定你知道'UITableView'是如何在**真正的''MVC'环境中工作的,请你先从ie ** [本教程](http://www.iosdevnotes.com)/2011/10/UITableView的教程/)**。 – holex 2012-08-10 11:03:16

回答

1

这里法齐尔....任何基本代码中的错误桌面和行增加

- (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; 
} 
+0

可以请您告诉何时行添加发生,同时点击编辑按钮或任何其他按钮@Tejeshwar Gill – fazil 2012-08-11 04:25:16