iphone - 在 UItableview 中添加一行

标签 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];   
  }

最佳答案

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

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

关于iphone - 在 UItableview 中添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900204/

相关文章:

ios - UITableView estimatedRowHeight 在重新加载时更改其偏移量

iphone - Objective-C 中的十六进制值

ios - 从通讯录打开自己的应用

iphone - 如何使用核心数据获取按距离排序的地点列表

ios - 更改工具栏高度时如何防止按钮向右移动?

objective-c - Facebook 登录对话框不适用于 UIAlertView,但适用于 UIButton

ios - 如何访问 tableView 的标准 viewForHeaderInSection?

swift - Firestore - UITableViewCell 内的 UISlider

ios - 从 UITableViewCell 标识符中获取类

iphone - 在选定的选项卡栏项目上设置文本颜色