用 uitableview 加载 UIView

Posted

技术标签:

【中文标题】用 uitableview 加载 UIView【英文标题】:Loading UIView with uitableview 【发布时间】:2016-05-02 20:57:56 【问题描述】:

您好,我想在您单击一行 uitableview 时加载一个 UIView。 我做了这个链接: ViewController Segue Xamarin 但是在打印中出现以下错误,Man.storyboard 也有模拟器。 下面是具有 uitableview 的视图代码以及 UITableViewSource 的代码。 如果有人可以提供帮助,我将不胜感激。

partial class ListaWodsFemininosViewController : BaseController2

    public ListaWodsFemininosViewController (IntPtr handle) : base (handle)
    
    

    public override void ViewDidLoad ()
    
        base.ViewDidLoad ();

        DetalheWodViewController detalhe = (DetalheWodViewController)Storyboard.InstantiateViewController("detalhe");

        List<WodReferencia> Wods = new List<WodReferencia> ();

        WodReferencia ref0 = new WodReferencia 
            Nome        = "Amanda",
            Feminino    = "Three rounds, 9-7- and 5 reps, of:\n" +
                "Muscle-up 135 pound Squat snatch.",
            Masculino   = "Three rounds, 9-7- and 5 reps, of: " +
                "Muscle-up 135 pound Squat snatch."

        ;
        Wods.Add (ref0);

        WodReferencia ref1 = new WodReferencia 
            Nome        = "Angie",
            Feminino    = "100 Pull-ups 100 Push-ups 100 Sit-ups 100 Squats.",
            Masculino   = "100 Pull-ups 100 Push-ups 100 Sit-ups 100 Squats."
        ;
        Wods.Add (ref1);

        WodReferencia ref2 = new WodReferencia 
            Nome        = "Annie",
            Feminino    = "Double-unders Sit-ups 50-40-30-20 and 10 rep rounds.",
            Masculino   = "Double-unders Sit-ups 50-40-30-20 and 10 rep rounds."
        ;
        Wods.Add (ref2);

        WodReferencia ref3 = new WodReferencia 
            Nome        = "Barbara",
            Feminino    = "20 Pull-ups 30 Push-ups 40 Sit-ups 50 Squats Rest precisely 3 minutes between each round. 5 rounds, time each round.",
            Masculino   = "20 Pull-ups 30 Push-ups 40 Sit-ups 50 Squats Rest precisely 3 minutes between each round. 5 rounds, time each round."
        ;
        Wods.Add (ref3);

        WodReferencia ref4 = new WodReferencia 
            Nome        = "Chelsea",
            Feminino    = "5 Pull-ups 10 Push-ups 15 Squats Each minute on the minute for 30 minutes.",
            Masculino   = "5 Pull-ups 10 Push-ups 15 Squats Each minute on the minute for 30 minutes."
        ;
        Wods.Add (ref4);

        WodReferencia ref5 = new WodReferencia 
            Nome        = "Christine",
            Feminino    = "3 rounds for time of: 500 m Row 12 BW Deadlift 21 Box Jumps.",
            Masculino   = "3 rounds for time of: 500 m Row 12 BW Deadlift 21 Box Jumps."
        ;
        Wods.Add (ref5);

        WodReferencia ref6 = new WodReferencia 
            Nome        = "Cindy",
            Feminino    = "5 Pull-ups 10 Push-ups 15 Squats As many rounds as possible in 20 minutes.",
            Masculino   = "5 Pull-ups 10 Push-ups 15 Squats As many rounds as possible in 20 minutes."
        ;
        Wods.Add (ref6);

        ...

        gridWods.Source = new FonteTabelaWodsFeminino (Wods,detalhe);
    

    public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
    
        if (segue.Identifier == "detalhe") 
            var detalhe = segue.DestinationViewController as DetalheWodViewController;
        
    


public class FonteTabelaWodsFeminino : UITableViewSource


    private List<WodReferencia> wodsFemininos; 
    DetalheWodViewController detLocal;
    private string cellIdentifier = "TableCell";

    public FonteTabelaWodsFeminino (List<WodReferencia> wodsF, DetalheWodViewController det)
    
        wodsFemininos = wodsF;
        this.detLocal  = det;
    

    public override nint RowsInSection (UITableView tableview, nint section)
    
        if (wodsFemininos != null) 
            return wodsFemininos.Count;
         else 
            return 0;
        
    

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
    
        UITableViewCell cell            = tableView.DequeueReusableCell (cellIdentifier); 
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);

        cell.TextLabel.Lines            = 0;
        cell.TextLabel.SizeToFit ();
        cell.TextLabel.LineBreakMode    = UILineBreakMode.WordWrap;
        cell.DetailTextLabel.Text       = wodsFemininos [indexPath.Row].Masculino;
        cell.TextLabel.Text             = wodsFemininos [indexPath.Row].Nome;
        return cell;
    

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    
        detLocal.PerformSegue("detalhe", detLocal);
    

【问题讨论】:

我没有看到您遇到的错误。你能用它更新帖子吗?错误发生在哪一行?如果需要帮助查找行,请使用异常断点。 请不要发布错误的屏幕截图。搜索引擎无法索引屏幕截图。花点时间复制帖子中的错误并对其进行格式化,使其易于阅读。 我按照上图进行了调整。在以下标识符中。但它仍然没有工作。该行给出的问题是: detLocal.PerformSegue ("detail", detLocal);方法:public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 错误是: 【参考方案1】:

认为你需要像这样给你的 segue 一个 ID:

刚刚注意到您使用的是 Xamarin ios 设计器,所以这里也是该设计器的屏幕截图;)

【讨论】:

以上是关于用 uitableview 加载 UIView的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 中未加载 JSON 数据

使用 RxSwift 异步加载 UITableView 单元格

覆盖不滚动 UITableView - iOS

在 uitableview 上加载更多向上滚动的项目

使用 plist 加载 UITableView 的内容

UITableView中默认重新加载后如何记住选择的行并维护它们?