【问题标题】:UITableView edit mode not workingUITableView 编辑模式不起作用
【发布时间】:2010-12-29 04:57:35
【问题描述】:

我正在尝试激活 UITableView 的编辑模式。当我单击“编辑”按钮时,它会变为“完成”,但圆形红色减号不会显示在表格中。我在下面发布了一些类的方法。应用程序委托创建了一个选项卡栏,选项卡内的视图具有导航控制器。一切都在代码中完成,几乎没有用 nib 文件完成。怎么了?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
    return [ikub.subscriptions count];  
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    int i = [indexPath row];  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];  
    if (cell == nil) {  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];  
    }  
    Subscriptions *s = [ikub.subscriptions objectAtIndex:i];  
    cell.textLabel.text         = [s title];  
    cell.detailTextLabel.text   = [NSString stringWithFormat:@"%@ artikuj te rinj, %@ artikujt te lexuar", [s new], [s read]];  
    if (deleteActive) {  
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;  
    } else {  
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
    }  
    return cell;  
}  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    Subscriptions *s = [ikub.subscriptions objectAtIndex:[indexPath row]];  
        NSString *address = [[NSString alloc] init];  
        NSString *type = [NSString stringWithString:[s type]];  
        if ([type isEqualToString:@"Category"]) {  
            address = [NSString stringWithFormat:@"http://www.ikub.al/Rss.aspx?node=9e26cdb8-dba6-4504-8bb6-29f0753be179~%@", [s code]];  
        } else if ([type isEqualToString:@"Tag"]) {  
            address = [NSString stringWithFormat:@"http://www.ikub.al/Rss.aspx?tag=%@", [s code]];  
        }
        address = [address stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];  
        SubscriptionStories *ss = [[SubscriptionStories alloc] init];  
        ss.address = address;  
        ss.title = [s title];  
        [self.navigationController pushViewController:ss animated:YES];  
        [ss release];  
}  
- (void)viewWillAppear:(BOOL)animated {  
    [subscriptionsTable reloadData];  
}  
- (void)viewDidLoad {  
    refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:nil];  
    self.navigationItem.leftBarButtonItem = refreshButton;  
    [refreshButton release];  
    self.navigationItem.rightBarButtonItem = self.editButtonItem;  
    [super viewDidLoad];      
}  
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    
    return YES;  
}

【问题讨论】:

  • 嘿,olsi,请通过正确格式化来编辑您的问题。它很难理解代码。方法的结束和开始之间没有空格。

标签: iphone objective-c uitableview ios


【解决方案1】:

继承对其没有任何影响。

您可以使用此方法,当您将导航控制器的右键分配给它时,它会起作用。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [tableView setEditing:editing animated:YES];
}

或者您可以编写一个基本上切换 UITableView 状态的方法,例如

[tableView setEditing:TRUE];

当按钮完成后,将其切换到

[tableView setEditing:FALSE];

【讨论】:

  • 我同意,以上投票,我使用通常的 UIViewController,它工作正常 - 相反你需要:1. 让你的控制器成为 UITableViewDelegate、UITableViewDataSource 的代表,2. 实现 - (void)setEditing: (BOOL)编辑动画:(BOOL)动画,3.以编程方式添加EDIT按钮 - self.navigationItem.rightBarButtonItem = self.editButtonItem
  • UITableViewController 有时过于有限,这就是我们使用 UIViewController 的原因。好帖子!谢谢。
【解决方案2】:

你没有正确继承。

你不会从 UIViewController 中得到任何东西,它只包含视图。我建议你试试 UITableViewController。

【讨论】:

  • 如果他有一个tableView对象并且UIViewController是它的Delegate,则与继承无关。
【解决方案3】:

你在哪里写的其他方法 uitable view 编辑方法 像这样

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        rowNumber = indexPath.row;
        UIActionSheet *registerActionSheet = [[UIActionSheet alloc] initWithTitle:@"Sure You Want To Delete?"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", nil];
        registerActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [registerActionSheet showInView:self.view];
        [registerActionSheet release];
        // delete your data item here
        // Animate the deletion from the table.
        //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

【讨论】:

    【解决方案4】:

    已修复!必须继承自 UITableViewController 而不是 UIViewController。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2014-11-18
      • 2018-07-19
      • 2011-06-05
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 2012-12-18
      相关资源
      最近更新 更多