【问题标题】:trouble with deleting uitableview rows by default method默认方法删除uitableview行的麻烦
【发布时间】:2012-09-07 10:15:08
【问题描述】:

这个 UITableViews 会让我发疯的!

我有 UITableViewCell 创建的

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
[cell addSubview:doneButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
[doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
postedTime.backgroundColor = [UIColor clearColor];
[cell addSubview:postedTime];
cell.textLabel.textColor = [UIColor blackColor];

UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 190, 30)];
[cell addSubview:post];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
[cell addSubview:title];

mission *m = (mission *)[missionsArray objectAtIndex:indexPath.row];
title.text = m.title;
post.text = m.post;
postedTime.text = m.posted;

.h 文件:

IBOutlet UITableView *table;
NSMutableArray *missionsArray;

我正在尝试按方法删除行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table reloadData];
}

当我点击“删除”按钮 EXC_BAD_ACCESS。我试图调用 [missionsArray removeAllObject]; ——同样的错误。然后我尝试了 NSLog(@"%u", indexPath.row); -- 它打印正确的行(当我选择第一行时,它返回 0 等等)。我也试过这个

if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        [table reloadData];
}

而且我没有好的结果。也许细胞高度会影响它...... 请帮忙。我不知道该做什么,在哪里搜索。


问题解决了。 当我试图释放我的任务对象时,麻烦出现了逻辑错误。

【问题讨论】:

  • 与您的问题无关...但是您是否有理由每次都“创建”按钮和额外的标签,而不是一次?
  • 也许是我的逻辑错误
  • 由于所有单元格都是相同的并且“可重复使用”,因此将所有按钮和标签的初始化代码移动到 if (cell == nil) { 块内,它将提高性能,(最好创建子类以保持整洁)

标签: iphone ios uitableview delete-row


【解决方案1】:
#import "ViewController.h"


@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        missionsArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C", nil];
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }


    #pragma mark TableView delegate method.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1 {
        return 1;
    }


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


    - (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 44;


    }
    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"myCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        }

       UIButton * doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
        [cell addSubview:doneButton];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

        UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        postedTime.backgroundColor = [UIColor clearColor];
        [cell addSubview:postedTime];
        cell.textLabel.textColor = [UIColor blackColor];

        UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 20, 190, 30)];

        post.text=[missionsArray objectAtIndex:indexPath.row];
        [cell addSubview:post];

        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
        [cell addSubview:title];

        return cell;

    }

        enter code here

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [missionsArray removeObjectAtIndex:indexPath.row];
            [table reloadData];
        }
    }




    @end

【讨论】:

  • 我正在填充任务数组,就像你的答案一样,我的 UItableView 有 1 个部分。它不工作:\
  • 抱歉,我是盲人。一会儿我会检查它。
  • 我认为你还没有连接 xib 中的表检查一次
  • 正确。它没有连接。但是在连接之后 - 没有任何反应。 EXC_BAD_ACCESS
  • 请复制我给出的表格视图的所有委托方法并将其粘贴到那里它将起作用请保留断点并检查一次
【解决方案2】:

假设您的TableView 中只有一个部分 试试这个:

if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        // No need to reload the tableview.  Remove this line entirely [table reloadData];
}

【讨论】:

    【解决方案3】:

    感谢您的帮助。我解决了这个问题。答案在更新后的问题中!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 2013-09-07
      • 2023-03-13
      • 2012-10-07
      • 2015-04-12
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多