我正试图从带有C#的ListBox中获取所有选定的项目ValueMember.
例如:
我有这样一个列表:
ID | Name and Lastname
----------------------
1 | John Something
2 | Peter Something2
3 | Mary Smith
Run Code Online (Sandbox Code Playgroud)
这个结构是我的ListBox的一部分.我用这段代码构建了这个列表框:
private void fill_people_listBox()
{
this.listBoxPeople.DataSource = db.query("SELECT ....");
this.listBoxPeople.DisplayMember = "people_name_last";
this.listBoxPeople.ValueMember = "people_id";
}
Run Code Online (Sandbox Code Playgroud)
ListBox已成功填充.当用户想要保存更改时,我将遍历此列表以获取所有选定的项目,但我不需要该项目,我需要ID.
例:
1 | John Something.
Run Code Online (Sandbox Code Playgroud)
忽略John Something,得1,所以我不需要DisplayMember,只需要ValueMember.
为此,我尝试了以下几种方法:
1º
foreach (ListBox selectedItem in this.listBoxGarantes.SelectedItems)
{
selectedItem.ToString(); // just an example, I'm printing this.
}
Run Code Online (Sandbox Code Playgroud)
2º
string strItem;
// insert garantes
foreach (object selectedItem in this.listBoxGarantes.SelectedItems)
{
strItem = selectedItem as String;
strItem; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Objective-C方法转换为Swift.
Objective-C方法如下:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
[[segue destinationViewController] setUrl:string];
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我遇到的Swift方法(有问题)如下:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "showDetail") {
var indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()
var string:NSString = self.feeds[indexPath.row] as String
string = string.stringByReplacingOccurrencesOfString("\n", withString: "")
segue.destinationViewController.setURL(string, forKey: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题是最后一行:
segue.destinationViewController.setURL(string, forKey: nil)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
"无法将表达式的类型'Void'转换为'NSURL!'"
这是我试图调用的控制器:
import UIKit
class …Run Code Online (Sandbox Code Playgroud)