如何使用pickerView和actionsheet向tableView添加一行
有什么想法吗?一些示例代码,或者一些建议将受到欢迎......谢谢
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你的想法是正确的。从逻辑上讲,您需要检查该对象是否已存在于表中(您已经在这样做)。然后,如果它不存在,您将插入它。如果您不太熟悉更新表视图,这就是可能出现问题的地方。您上面所做的就是正确的开始。您想要创建一个要在其中插入新对象的索引路径,但在实际插入新行之前需要执行一些步骤。您必须让
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 havebanks
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 callinsertRowsAtIndexPaths:withRowAnimation:
. That way when the tableView calls itsdataSource
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