Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITABLEVIEWCELL getting info from selected row from a label.

PatrickNJPatrickNJ NjPosts: 11New Users *
Hi guys
I have a tableview in uiviewcontroller, it has a custom cell where I set label to get the value from my mysql database. everything works ok but I want to get when the user tap in a row, to go to another uiviewcontroller according the info I have in the label ID. Im going to place the code Im using, question is: this is the right way to do it? I was wondering when the user tap in the row Im creating only one instance of label and reusing it or it's creating a new one every time the user tap the row and make memory leaks in my app?

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *rowSelected = [tableView cellForRowAtIndexPath:indexPath];
UILabel *checkSelected = (UILabel *)[rowSelected viewWithTag:1];
if ([checkSelected.text isEqualToString:@id: 1])
{
// ... do my code
}
else
{
// ... return msg uialert
}
}
Tagged:

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    1. No, this is not the right approach. You should consider cell elements to be for display only, and should not be used for making decisions. You should use the index path to select appropriate data, and then use that data to make decisions.

    2. You are not creating anything with this code. You are referencing items that already exist.

    3. You might want to read through the table view link in my signature.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    No, that is not the way to do it. You should not query the data in the table view. The table view is a VIEW object. It's for presenting information to the user, not saving state.

    You should use the row (or row and section, if you're using sections) to query your model and decide what to do from there. If your data is in an SQL database then you would ask the database for the value at that index.

    Your code does not have any memory leaks; You are fetching the value of a label in an existing table view cell, so you are not creating a new object.

    However, table view cells are transient. As soon as the user scrolls to a different part of your table, that cell will scroll of the screen and eventually be recycled to display data for a different entry in your model.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    sorry, actually I kind didn't explain it right... the label is holding data from my database not to get it, lets say I open my uiview and it loads data into my uitableview from my server, then the row description will hold the item description and Label ID will hold the item id from my mysql server after loads the information, then lets say if the row 0 has id label = newPeople then... will open a new uiview of new people after the user tap in this row. what I mean is every item will have id for sure because it will be a list of services that will not change.

    so, looking at this way, is that correct way to compare when the user tap the row? (label == string) code above.
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    PatrickNJ wrote: »
    sorry, actually I kind didn't explain it right... the label is holding data from my database not to get it, lets say I open my uiview and it loads data into my uitableview from my server, then the row description will hold the item description and Label ID will hold the item id from my mysql server after loads the information, then lets say if the row 0 has id label = newPeople then... will open a new uiview of new people after the user tap in this row. what I mean is every item will have id for sure because it will be a list of services that will not change.

    so, looking at this way, is that correct way to compare when the user tap the row? (label == string) code above.

    My previous post still stands. No, it is not the correct way. You should not store data in view objects. It's bad design. If your data comes down from a remote server, cache it locally in an array. Use the array to compare and see if the strings match.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    edited October 2013
    sure Duncan... Thanks for the advice.. if you don't mind another question, I still quite confused.. sorry Im just doing my first app and working with ios about 4 months only... anyway, you're saying I should hold my data in array and then put it in a tableview? if yes, what could be the best option for me after the tableview receives the list data, to recognize what type of service the user tap/selected into the tableview... I dont want the ID of the service to be showing in the tableview, only the description. So lets say he selected row 2 has service Add New it will open the uiview for add new. I understand it would be just fine create a UIbutton and set segue to the view and lets say user click button1 goes to new, button 2 goes to view add..... but my list is kind of big it would be a lot of button, don't like it.

    Thanks again..
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Your explanations really are not very good. Show specific examples of what you have and what you want.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    I would create model objects and put the necessary data within those. Then create an array of those model objects that will be used to populate the information in your tableview. By using the selected index of a cell selected, you can retrieve the proper object with all of the data you need. Once you have the object you can determine the type of segue to perform. It sounds like you should look into MVC pattern in iOS.
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    edited October 2013
    Brian. what I have is a tbale in my server with about 50 items with ID and Description. what I want is put this services in a tableview and when the user select the service Add New, I dont know in what row it will be because the list can grown, and the description can change any time, thats why i have ID for each one. so he selected Add New, i need to check what is the ID of the item user selected and based on this information I'll open the uiview for this item. so user select Add new, when he tap in this item code will see what Id is, lets say the id for the item selcted is ADN001, then comes if statment if row selected has ID == ADN001 will open the uiview for Add New. (but i dont want to show the id info on my tableview)
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    So if you were to make a model object to describe one of these services, would it look something like this?
    @interface PNJService : NSObject
    
    @property (nonatomic, copy) NSString *identifier;
    @property (nonatomic, copy) NSString *serviceDescription;
    
    @end
    
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    Yes something like that.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Ok. How are you currently putting information into the cell in the first place? Show your cellForRow.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    ok. I was doing it manually just adding the items thru the code to see how it could work then to connect to my db..

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@MainCell];
    UILabel *labelID = (UILabel *)[cell viewWithTag:1];

    if (cell == nil)
    {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@MainCell];
    }
    if (indexPath.row == 0)
    {
    cell.textLabel.text = @Description Test;
    labelID.text = @IDTEST;
    }
    else if (indexPath.row == 1)
    {
    cell.textLabel.text = @..;
    }
    ....
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return 5;
    }
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Ok, no. Just no. Go read the table view link in my signature. All of it. I'm not going to help you anymore here until you convince me that you've read it.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    then i was checking the info this way:
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *rowSelected = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *checkSelected = (UILabel *)[rowSelected viewWithTag:1];
    if ([checkSelected.text isEqualToString:@id:IDTEST])
    {
    // ... do my code
    }
    else
    {
    // ... return msg uialert
    }
    }
  • TanderTander South AfricaPosts: 243New Users @ @
    Okay I think you need to have a better understanding of how Tableviews work and their cells, too. Did you read Brain's table view link in his signature?

    Ultimately, you're having issues because you don't appear to understand the fundamentals of UiTableView.

    Once you understand exactly how they work - working with them becomes less of a pain. Much less.
  • PatrickNJPatrickNJ NjPosts: 11New Users *
    edited October 2013
    Tander... you're right, I got it working now using cell information, index path, it really became less of a pain :)... and it was little bit more difficult because I am using the tableview in a UIViewController, so it took little longer to get all the context. thank you guys for all advices.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Well, show what you have now. Let's see what you're doing. And use code tags.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • TanderTander South AfricaPosts: 243New Users @ @
    As BrianSlick mentioned, showing us your code helps in two ways:

    1. We can see if there is room for improvement or if there is something you're doing that might not be the best thing - you learn more.
    2. Others who stumble across this post looking for answers might find your code snippet useful.
Sign In or Register to comment.