Yes, it's clousure stuff. If you want you can see why that happens here:
http://www.defmacro.org/ramblings/fp.html#part_11 although I recommend the
whole article for everybody, it's pretty nice.



2013/3/5 Nic Wise <[email protected]>

> Hi Iki
>
> for (int i = 0; i < newsList.Count; i++)
>          {
>             StringElement se = new StringElement(newsList[i]);
>             section.Add(se);
>
>             int localCounter = i; //grab it, 'cos you want to use it
> inside the lamda, and "i" may have the wrong scope
>             se.Tapped += () =>
>             {
>                //DialogViewController with modified ctor (string name)
>                  DetailViewController dvc = new
> DetailViewController(newsList[localCounter]); //use the local one
>                  this.NavigationController.PushViewController(dvc,true);
>              };
>
>              stringElementsList.Add(se); //you dont need this, you are
> storing it in the section above!
>           }
>
> you might not need the localCounter bit (you might just be able to use
> i) - try it out. If it always shows the last element, regardless, then
> use the localCounter bit. I know you need to do this for objects, just
> not 100% sure on value types :)
>
> you could rewrite it like this, too:
>
>
>
> newsList = new List<string>()
>         {
>             "News - 1"
>            ,"News - 2"
>            ,"News - 3"
>            ,"News - 4"
>          };
>
> foreach(var s in newsList)
> {
>
> string temp = s; //capture outside, use inside the lambda/delegate
>
> section.Add(new StringElement(s, () => {
>   DetailViewController dvc = new DetailViewController(temp);
>    this.NavigationController.PushViewController(dvc,true);
> });
>
>
> }
>
>
>
>
> On 5 March 2013 20:37, Iki <[email protected]> wrote:
> > Hey Juan,
> >
> > Thanks for such quick response, as a matter of fact, my approach was
> exactly
> > like Your second solution.
> > But along the way I have made a mistake while assigning a new
> > DialogViewController, here is the example of working solution:
> >
> > public partial class MainViewController : DialogViewController
> > {
> >     List<string> newsList;
> >     List<StringElement> stringElementsList;
> >
> >     public MainViewController () : base (UITableViewStyle.Grouped, null)
> >     {
> >
> >        Root = new RootElement ("Main View") {};
> >        Section section = new Section ("");
> >
> >
> >         newsList = new List<string>()
> >         {
> >             "News - 1"
> >            ,"News - 2"
> >            ,"News - 3"
> >            ,"News - 4"
> >          };
> >
> >          stringElementsList = new List<StringElement> (){};
> >
> >          for (int i = 0; i < newsList.Count; i++)
> >          {
> >             StringElement se = new StringElement(newsList[i]);
> >             section.Add(se);
> >
> >             se.Tapped += () =>
> >             {
> >                //DialogViewController with modified ctor (string name)
> >                  DetailViewController dvc = new
> > DetailViewController(newsList[se.IndexPath.Row]);
> >                  this.NavigationController.PushViewController(dvc,true);
> >              };
> >
> >              stringElementsList.Add(se);
> >           }
> >
> >           Root.Add (section);
> >
> >
> >      }
> > }
> >
> >
> >
> > On Mar 5, 2013, at 8:11 PM, Juan M Gómez <[email protected]> wrote:
> >
> > Hi Iki,
> > There are a few ways to go on:
> > ->Just a specialized class of StringElement that exposes your properties
> or
> > a new event.
> > ->If you assign the data in the same struct, just use it (if it is a
> loop,
> > remember to set a new assign in the block of lambda)
> >
> > cheers
> >
> >
> > Juan M Gómez
> >
> >
> > 2013/3/5 Iki <[email protected]>
> >>
> >> Hi guys/gals,
> >>
> >> I'm having trouble accessing MonoTouch Dialog elements.
> >>
> >> Example is:
> >>
> >> I have a list of items from which i create StringElements in section.
> >> After elements is tapped, i would like to go to a new view, on which i
> >> would, based on the element tapped filled out the data.
> >>
> >> StringElement.Tapped is without parameters,
> >>
> >> se.Tapped += () =>
> >>                 {
> >>
> >>
> >>                 };
> >>
> >>  and i can't find a proper way to send reference of the object that is
> >> tapped.
> >>
> >> I'm currently using DialogViewControllers, for my main view, on which i
> >> create list of name, and on my detail view which i need to fill up with
> data
> >> based on element from the main view.
> >>
> >> I did try with this
> >>
> >> public void RowSelected (UITableView tableView, NSIndexPath indexPath)
> >> {
> >>     DetailViewController dvc = new DetailViewController (names
> >> [indexPath.Row]);
> >>     this.NavigationController.PushViewController (dvc, true);
> >> }
> >>
> >> But I don't see any effect.
> >>
> >> Thanks!
> >>
> >>
> >>
> >> _______________________________________________
> >> MonoTouch mailing list
> >> [email protected]
> >> http://lists.ximian.com/mailman/listinfo/monotouch
> >>
> >
> > _______________________________________________
> > MonoTouch mailing list
> > [email protected]
> > http://lists.ximian.com/mailman/listinfo/monotouch
> >
> >
> >
> > _______________________________________________
> > MonoTouch mailing list
> > [email protected]
> > http://lists.ximian.com/mailman/listinfo/monotouch
> >
>
>
>
> --
> Nic Wise
> t.  +44 7788 592 806 | @fastchicken
> b. http://www.fastchicken.co.nz/
>
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to