2016-05-13 4 views
0

Ich ziehe Daten in TextBoxen für meine App mit einem Index von '{' und ich kann nicht scheinen, die letzte Box (BeispielBox) zu bekommen, um das letzte Bit der Daten nach der letzten '{' in die richtige einzugeben Stattdessen wiederholt es das Beschreibungsfeld und fügt den Beschreibungstext in das Feld Beispiel sowie das Feld Beschreibung ein.C# BracePos Schwierigkeit?

using Foundation; 
using System; 
using System.CodeDom.Compiler; 
using UIKit; 
using System.IO; 
using System.Drawing; 
using CoreGraphics; 

namespace SQLCheatSheetProto 
{ 
    partial class FourthScreenViewController : UIViewController 
    { 
     public FourthScreenViewController (IntPtr handle) : base (handle) 
     { 
     } 

     public override void ViewDidLoad() 
     { 

      SearchText.ShouldReturn += (sender) => { 
       SearchText.ResignFirstResponder(); 
       LoadCommand(SearchText.Text.ToLower());  //If return is pressed while typing in the search box, this acts as though the user has pressed the physical Search button 
       return true; 


      }; 
     } 

     public void LoadCommand(string commandName) 
     { 
      bool commandFound = false; 
      foreach (string file in Directory.GetFiles("/Users/lcharlton/Documents/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetFiles")) { 
       StreamReader reader = new StreamReader (file); 
       bool endOfFile = false; 
       while (!commandFound && !endOfFile) { 
        string s = reader.ReadLine(); 
        int bracePos = s.IndexOf ("}"); 
        if (bracePos >= 0) { 
         if (s.Substring (0, bracePos).ToLower() == commandName) {        //Searches through every .csv in the specified path and returns the first command that has a command name mathcing what the user specified. 

          s = s.Substring (bracePos + 1); 
          bracePos = s.IndexOf ("}"); 
          SyntaxText.Text = s.Substring (0, bracePos); 
          DescriptionText.Text = s.Substring (bracePos + 1); 
          ExampleText.Text = s.Substring (bracePos + 1); 

          DescriptionText.Font = SyntaxText.Font; 
          commandFound = true; 
         } 
        } 
        if (reader.EndOfStream) { 
         reader.Close(); 
         endOfFile = true; 
        } 
       } 
      } 
     } 

     partial void SearchClick (UIButton sender) 
     { 
      try{ 
       LoadCommand(SearchText.Text.ToLower());} 
      catch{} 
     } 
    } 
} 

Das ist mein SecondViewController Mein erstes ist:

using Foundation; 
using System; 
using System.CodeDom.Compiler; 
using UIKit; 
using System.IO; 
using System.Drawing; 
using CoreGraphics; 
using System.Collections.Generic; 

namespace SQLCheatSheetProto 
{ 
    partial class SecondScreenViewController : UIViewController 
    { 
     public SecondScreenViewController (IntPtr handle) : base (handle) 
     { 
     } 

     string path = "/Users/lcharlton/Documents/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetFiles"; //file path where the .csv files are stored on the machine (Can be replaced with something like Directory.CurrentFilePath() though you would need to move the .csvs there) 

     CGRect screen = UIScreen.MainScreen.Bounds; 
     List<UIButton> buttons = new List<UIButton>(); 


     static CGRect scrollerFrame; //scroller initialization 
     static UIScrollView scroller; 

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

      //scroller.ContentSize.Height = screen.Size.Height; 


      scrollerFrame = new CGRect (0, 160, screen.Width, screen.Height - 160); 
      scroller = new UIScrollView (scrollerFrame); 
      View.AddSubview (scroller); 




      View.SendSubviewToBack (scroller); //Scroller is added to the main screen so that buttons can then be added to the scroller 



      int buttonHieght = 0; 
      int screenWidth = Convert.ToInt32(screen.Width); 

      foreach (string file in Directory.GetFiles(path)) 
      { 
       string filename = file.Replace (path + "/", ""); 
       filename = filename.Replace (".csv", ""); 
       if(filename != ".DS_Store"){ 
       //add a button 
       var btn = UIButton.FromType (UIButtonType.System); 
        btn.Frame = new CGRect (0, buttonHieght, screenWidth,44);    //Goes to the file path specified and reads all filenames; displaying them as buttons on-screen 
       btn.SetTitle (filename, UIControlState.Normal); 

       scroller.Add (btn); 

       buttons.Add (btn); 

       buttonHieght = buttonHieght + 50; 

       btn.TouchUpInside += (sender, e) => //each button generated waits on the event of being tapped and runs this code if that happens 
        { 
        LoadCommands(btn.CurrentTitle); 
        }; 
       } 
       if (buttonHieght >= screen.Height-1000) 
       { 
        CGSize size = new CGSize (0, 45); 
        scroller.ContentSize = scroller.ContentSize + size;     //Allows the scroll view to adapt it's content size in order to allow scrolling through a list of buttons 
       } 

      } 
     } 

     public void LoadCommands(string buttonPressed) 
     { 
      foreach (UIButton button in buttons) 
      { 
       //delete all buttons in buttons<> 
       button.Hidden = true; 
      } 

      buttons.Clear(); 

      StreamReader reader = new StreamReader (path + "/" + buttonPressed + ".csv"); 
      bool commandsLoaded = false; 
      int buttonHieght = 0; 
      int screenWidth = Convert.ToInt32(screen.Width); 
      while (!commandsLoaded) 
      { 
       var btn = UIButton.FromType (UIButtonType.System); 
       btn.Frame = new CGRect (0, buttonHieght, screenWidth, 44);  //Finds all command names in the .csv selected and generates buttons based on the names. ('}' is used as a delimiter in the .csvs) 
       string commandRecord = reader.ReadLine(); 
       int bracePos = commandRecord.IndexOf ("}"); 
       string commandName = commandRecord.Substring (0, bracePos); 
       btn.SetTitle (commandName, UIControlState.Normal); 

       scroller.Add (btn); 
       btn.TouchUpInside += (sender, e) => { //waits for the button to be pressed to run the code 
        DisplayCommand(btn.CurrentTitle, buttonPressed); 
       }; 
       buttons.Add (btn); 
       buttonHieght = buttonHieght + 50; 
       if (reader.EndOfStream) { 
        commandsLoaded = true;   //when the StreamReader reaches the end of the file, it exits the loop 
        reader.Close();    
       } 

       if (buttonHieght >= screen.Height-1000) 
       { 
        CGSize size = new CGSize (0, 45);   //extends content size of the scroll view in order to add more buttons as needed 
        scroller.ContentSize = scroller.ContentSize + size; 
       } 
      } 
     } 

     public void DisplayCommand(string lastButton, string file) 
     { 
      foreach (UIButton button in buttons) 
      { 
       button.Hidden = true; 
      } 

      bool commandFound = false; 
      StreamReader reader = new StreamReader (path + "/" + file + ".csv"); 
      while (!commandFound) 
      { 
       string commandRecord = reader.ReadLine(); 
       int bracePos = commandRecord.IndexOf ("}"); 
       string commandName = commandRecord.Substring (0, bracePos); 

       if (commandName == lastButton) 
       { 
        var nameBox = new UITextField(); 
        var syntaxBox = new UITextView(); 
        var descripBox = new UITextView(); 
        var exampleBox = new UITextView(); 
        nameBox.Frame = new CGRect (25,160, screen.Width - 50, 50);  //splits the line of the selected command up into it's separate parts NAME}SYNTAX}DESCRIPTION 
        syntaxBox.Frame = new CGRect (20, 220, screen.Width - 50, 100); 
        descripBox.Frame = new CGRect (20, 300, screen.Width - 50, 150); 
        exampleBox.Frame = new CGRect (20, 380, screen.Width - 50, 50); 

        nameBox.Text = commandName; 

        commandRecord = commandRecord.Substring (bracePos + 1); 

        bracePos = commandRecord.IndexOf("}");       //Each separate piece is assigned to the text value of a textview or textfield 

        syntaxBox.Text = commandRecord.Substring (0, bracePos); 

        commandRecord = commandRecord.Substring (bracePos + 1); 

        bracePos = commandRecord.IndexOf("}"); 

        descripBox.Text = commandRecord; 

        commandRecord = commandRecord.Substring (bracePos + 1); 

        exampleBox.Text = commandRecord; 




        scroller.Hidden = true; 

        View.AddSubview (nameBox); 
        View.AddSubview (syntaxBox);         //The text is then added to the viewcontroller, NOT THE SCROLLER. 
        View.AddSubview (descripBox); 
        View.AddSubview (exampleBox); 


        commandFound = true; 
        reader.Close(); 
       } 

       File.OpenRead("/Users/lcharlton/Documents/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetFiles"); 
       } 
     } 
    } 
} 

ich ein starkes Gefühl, es ist etwas, haben daher mit dem zweiten View-Controller zu tun, warum ich es an erster Stelle, wenn man helfen könnte, dass‘ d ist toll :)

Vielen Dank im Voraus :).

+0

Müssen Sie den Text zwischen '{' und '}' prüfen? Sie verwenden jedesmal '.IndexOf ("} ");' das andere '{' ist nicht markiert. –

+0

Ich verwende nur} als Trennzeichen innerhalb der Daten, da es sich um künstliche Daten handelt. –

+0

Kann mir jemand helfen? –

Antwort

0

Sie platziert den gleichen Text in Beschreibungsfeld und Beispielbox:

DescriptionText.Text = s.Substring(bracePos + 1); 
ExampleText.Text = s.Substring(bracePos + 1); 

Korrekter Code wäre dies:

s = s.Substring(bracePos + 1); 
    bracePos = s.IndexOf("}"); 
    SyntaxText.Text = s.Substring(0, bracePos); 

    s = s.Substring(bracePos + 1); 
    bracePos = s.IndexOf("}"); 
    DescriptionText.Text = s.Substring(0, bracePos); 

    s = s.Substring(bracePos + 1); 
    ExampleText.Text = s; 

Stellen Sie sicher, dass die Verstrebung Position jedes Mal ist> -1.

Es wäre einfacher, die Zeichenfolge mit } zu teilen. Dann haben Sie eine Sammlung von Strings, mit denen Sie die Textfelder füllen können.

public void LoadCommand(string commandName) 
    { 
     bool commandFound = false; 
     foreach (string file in Directory.GetFiles("/Users/lcharlton/Documents/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetProto/SQLCheatSheetFiles")) 
     { 
      StreamReader reader = new StreamReader(file); 
      bool endOfFile = false; 
      while (!commandFound && !endOfFile) 
      { 
       string s = reader.ReadLine(); 
       string[] items = s.Split(new[] { '}' }, StringSplitOptions.RemoveEmptyEntries); 
       if (items.Length > 0) 
       { 
        if (items[0].ToLower() == commandName) 
        {        
         //Searches through every .csv in the specified path and returns the first command that has a command name mathcing what the user specified. 
         SyntaxText.Text = items.Length > 1 ? items[1] : string.Empty; 
         DescriptionText.Text = items.Length > 2 ? items[2] : string.Empty; 
         ExampleText.Text = items.Length > 3 ? items[3] : string.Empty; 

         DescriptionText.Font = SyntaxText.Font; 
         commandFound = true; 
        } 
       } 
       if (reader.EndOfStream) 
       { 
        reader.Close(); 
        endOfFile = true; 
       } 
      } 
     } 
    }