如何使用pickerView和actionsheet向tableView添加一行

发布于 2024-11-17 21:39:40 字数 1590 浏览 4 评论 0原文

有什么想法吗?一些示例代码,或者一些建议将受到欢迎......谢谢

// add bank to tableView
-(void)addBankFromList {

    if (notInList == nil) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Could not be added" message:@"Bank already in list" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

        [alert show];
        [alert release];

    }

    else {

//       [banksTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[banks count] inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
//        [banksTableView reloadData];
    }
}

// call addBankFromList method when button Add to List is touched .
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    [self addBankFromList];
}

#pragma mark UIPicker Delegate and DataSource methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    for (NSString *temp in banksNotInList) 
        if (temp == [arrayWithBanks objectAtIndex:row]) {

            notInList = [[NSMutableString alloc]initWithString:temp];


            NSLog(@"%@",temp);
        }
        else notInList = nil;
}

已解决(阅读第一条评论)

[appDelegate.banks insertObject:notInList atIndex:[appDelegate.banks count]];
        [banksTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[banks count] inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

        [banksTableView reloadData];

any ideas ? some sample code , or some advices will be welcome ... thx

// add bank to tableView
-(void)addBankFromList {

    if (notInList == nil) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Could not be added" message:@"Bank already in list" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

        [alert show];
        [alert release];

    }

    else {

//       [banksTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[banks count] inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
//        [banksTableView reloadData];
    }
}

// call addBankFromList method when button Add to List is touched .
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    [self addBankFromList];
}

#pragma mark UIPicker Delegate and DataSource methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    for (NSString *temp in banksNotInList) 
        if (temp == [arrayWithBanks objectAtIndex:row]) {

            notInList = [[NSMutableString alloc]initWithString:temp];


            NSLog(@"%@",temp);
        }
        else notInList = nil;
}

SOLVED (read first comment)

[appDelegate.banks insertObject:notInList atIndex:[appDelegate.banks count]];
        [banksTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[banks count] inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

        [banksTableView reloadData];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2024-11-24 21:39:40

看起来你的想法是正确的。从逻辑上讲,您需要检查该对象是否已存在于表中(您已经在这样做)。然后,如果它不存在,您将插入它。如果您不太熟悉更新表视图,这就是可能出现问题的地方。您上面所做的就是正确的开始。您想要创建一个要在其中插入新对象的索引路径,但在实际插入新行之前需要执行一些步骤。您必须让 numberOfRowsInSection 与更新后的数字匹配,因此如果您要绘制从数组返回的值,则需要更新该数组。因此,假设您有 banks 作为要显示 tableView 单元格的数组,您可以将新对象插入到数组中的相应位置,然后调用 insertRowsAtIndexPaths:withRowAnimation:insertRowsAtIndexPaths:withRowAnimation:.这样,当 tableView 调用其 dataSource 方法时,就不会产生冲突的结果。简而言之,您更新数据数组,然后更新 tableView。我希望一切都有意义

It looks like you've got the right idea. Logically, you will want to check to see if the object already exists in the table (which you're already doing). Then if it's not in, you'll insert it. That's where trouble can arise if you're not too familiar with udpating tableViews. What you're doing above is the right start. You want to create an indexPath where you'd like to insert the new object, but there are a few steps you want to take before actually inserting the new row. You must have your numberOfRowsInSection match up with the updated number, so if you are drawing that return from an array, you need to update the array. So assuming you have banks as your array from which you're displaying the tableView cells, you would insert the new object into the array at its respecitve position, then call insertRowsAtIndexPaths:withRowAnimation:. That way when the tableView calls its dataSource methods, it won't have conflicting results. So in short, you update your data array, then update the tableView. I hope that all makes sense

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文