我写了:
func initializeMyDictionary() {
self.example = [MyDictionary(name: "Some Name", text: "some text", link: "linkToSomeDocument"),
MyDictionary(name: "Second Name", text: "some text", link: "secondLink")]
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier: String = "tableCell"
var cell: TableCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? TableCell
if cell == nil {
cell = TableCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)
}
cell!.nameLabel!.text = example[indexPath.row].name
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return example.count
}
然后将其链接到Main.storyboard上的DetailViewController(由我创建)。每个myDictionary字符串的DetailView都显示在tableView中。一切正常。
但是我想让我看一下MyDictionary(链接)的第二个值。第1行显示MyDictionary的“名称”和“文本”,第2行显示“ link”的文档或URL。这是不可能的,还是该怎么做?
我为无知和英语不好而感到抱歉。
两种方法:
1。
删除从原型单元到视图控制器的情节提要脚本,只需实现UITableViewDelegate
method-(void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath
方法,然后将另一个视图控制器推入那里,例如:
- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
// the identifier of the viewController in storyboard
NSString* identifier = nil;
// section 0
switch (indexPath.section) {
case 0:
// rows in section 0
switch (indexPath.row) {
// row 0 in section 0
case 0: identifier = @"IdentifierOfVCForFirstCellInFirstSection"; break;
case 1: identifier = @"IdentifierOfVCForSecondCellInFirstSection"; break;
default: break;
}
break;
default: break;
}
//
if (identifier != nil) {
// perform Segue programmatically
[self.navigationController pushViewController: [[self storyboard] instantiateViewControllerWithIdentifier: identifier] animated: YES];
}
else {
NSAssert(NO, @"No storyboard identifier implemented for row %@ in section %@", @(indexPath.row), @(indexPath.section));
}
}
或2。
在IB中的tableView中添加(拖放)另一个原型单元。在Attributes Inspector中给它一个不同的Reuse Identifier。然后将其与您想要的其他viewController连接(右键单击并将其'Triggered Segues'
拖到selection
所需的视图控制器下。
然后,在cellForRowAtIndexPath
method中,UITableViewCell
根据indexPath决定要拔出哪种魔杖。
对于第二个解决方案,我建议对原型单元使用自定义UITableView子类/ Xib。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句