How can I know which TableView?

Discussion in 'iOS Programming' started by mikezang, Jul 18, 2010.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I have two TableView in a TableViewController as below, how can I know which TableView in cellForRowAtIndexPath?

    Code:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";
    	
    	CustomCell *cell = (CustomCell *)[tableView	dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    	
    	if (cell == nil) {
    		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    		
    		for (id oneObject in nib) {
    			if ([oneObject isKindOfClass:[CustomCell class]]) {
    				cell = (CustomCell *)oneObject;
    			}
    		}
    	}
    
    	NSUInteger row = [indexPath row];
    	NSDictionary *rowData = [self.computers objectAtIndex:row];
    	cell.colorLabel.text = [rowData objectForKey:@"Color"];
    	cell.nameLabel.text = [rowData objectForKey:@"Name"];
    						
    	return cell;
    }
     

    Attached Files:

  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The first parameter to the method is a pointe to the table view calling the method. Compare this pointer with the IBOutlet pointer you have to the tables you created in IB. If you do not have outlets linked to the two table views create some.
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    I am sorry that I don"t what I can do, can you help me in more detail?
     

    Attached Files:

  4. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    I want to the top tableview only show one row for header, bottom tableview for detail rows and scrollable.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    I'm not about to download your code and write it for you. Neither am I going to post code here for you to copy and paste: you learn nothing by simply copying; rather you learn by working it out yourself and doing. I told you exactly how what to do.
     
  6. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    Well, I have three class, CellsAppDelegate, CellsViewController and CustomCell, can you tell me where I can define outlet?
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    You need access to the outlet to compare with the parameter passed to tableView:cellForRowAtIndexPath: so I would suggest that if the instance that this is defined in is in the xib then in there. Otherwise in your controller class and pass the pointer where it is needed.
     
  8. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #8
    Thanks for your advise, now I got what I need.

    Hope that you can continue to give better suggestion and tips:)
     

    Attached Files:

  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  10. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #10
    I hope there is a fixed header at top of TableView, though I can use a TextView, even so I still have to use a couple of Labels to show different header.

    By the way, do you have any good idea?
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Why not just take advantage of UITableView's built-in header capability?
     
  12. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #12
    I didn't know there is a header in UITableView, can that header be fixed when scroll table view rows?

    Can you tell me how to use it?
     
  13. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #13
    Well, you might talk about section header?

    That isn't what I need because that header will scroll together with data below it.

    You will see My Table view has a fixed header.
     

    Attached Files:

  14. macrumors 603

    Joined:
    Aug 9, 2009
    #14
    Read the reference docs for UITableView and look for the tableHeaderView property.

    Also look for UITableViewStylePlain and how it causes section headers to float, as described in the "Overview" subsection of the reference doc.
     
  15. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #15
    Thanks! I never found a sample to use this property.

    Where do I set the header? in viewDidLoad? or in
    Code:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Where can I find a sample code to use this property?

    Then I have a couple of cells in row, how can I set tableheader with multiple cells?
     
  16. macrumors 603

    Joined:
    Aug 9, 2009
    #16
    What have you tried?

    Seriously, describe what you've tried (search terms, whatever), because it was trivial to find an example and explanation simply by googling for UITableView tableHeaderView.

    If you're having this much trouble simply finding things, then you need to explain exactly how you're trying to find things. Coming here for every little thing is not an effective strategy in the long term. You need to learn how to find things effectively by yourself.

    There may be technical reasons why you're not finding what I found so simply. That's why you should explain exactly what you're doing.


    A tableHeader is a UIView, so you can make it be anything that fits in a UIView. Did you read the doc? If you know the basics of UIView, then what to do should be obvious. It's the most basic pattern in programming: Composition. That is, building up by assembling smaller simpler components into composite structures.

    If you're looking for me to provide a step-by-step complete example, I won't. Once again, you need to explain your search strategy for finding things. If everything you do needs someone to give you a complete sample on demand, then your strategy is flawed. You need to be more self-sufficient, either by learning how to write code without a complete sample, or by learning how to effectively search for samples.
     
  17. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #17
    Thanks for your Sermon・Preach・Harangue.

    I googled "how to use uitableview tableheaderview to float", but I couldn't find useful information.

    That would be the best if there is a complete sample code, but I just want to a sample hint for using that property.

    I read almost programming guide and information at apple site, but practice is not the same as just reading, it should be not easy for new feature if there is no any sample code.

    Anyway, I have to thank your for your long text, that mean you are a nice guy, but not a good teacher.
     
  18. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #18
    I 100% agree with chown33. Providing people with, what they euphemistically call, sample code does not teach them anything. They copy and paste it and don't understand a single thing about what they are doing. The documentation Apple provide is excellent and is more than enough to write simple features like this without any code to steal from.

    I disagree that chown33 has not a good teacher, to get you to learn on your own, to improve your abilities to use the documentation and resources that are available to you (a key programming skill) and to generally enable you to solve more advanced problems in the future. You have been a bad student in wishing to have the answer provided to you, spoon fed to you and generally all effort on your part removed.

    He even provided you with a suggested Google search term that you then didn't use!
     
  19. macrumors 603

    Joined:
    Aug 9, 2009
    #19
    You're using too many words.

    Also, the words are unrelated. A tableHeaderView is not the element that floats. Section headers float. Read the reference docs.

    If you remove the "to float", you should find an example at Cocoa With Love.


    How to change your search strategy to be more effective:

    1. Start with fewer words.
    2. If results are too general, add a relevant word. It's an error to add an irrelevant word.
    3. If results are poor quality, try removing a word. Remove less significant words first (such as "float", "use", etc.)
    4. Don't stop after one search. Add or remove words.
    5. Add the word "sample", "example", or "source" if you are looking for example code.

    You should learn which words are relevant by reading the reference docs. That's where you should always start, so you know what terms are relevant for searching.
     
  20. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #20
    Thanks for your suggestion.

    In fact, in many case, I ask here after I searched google and without a good result. But you're right, my search words might be not good enough, many times I used too words, I will try in simple key words.

    Back to my detail question, I thought the two tableviews is not so bad because I can use one custom cell in both header and detail, and I can connect them to outlets in IB.

    I am still not sure about tableHeaderView, but it looks like I can't use it in IB, I have to use it in programming, that is little difficult than in IB.
     
  21. macrumors 603

    Joined:
    Aug 9, 2009
    #21
    When you ask a question here, post the search terms you used. This tells us what you tried. If we see problems with your search terms, then we can suggest improvements. If you don't tell us your search terms, and they are giving you poor results, then you'll never be able to improve them and become more independent.

    It's the same process as when someone says their code doesn't work, but doesn't post their code. We have no way of knowing where the problem in the code is, because we can't see the code that has the problem.

    All of programming is problem-solving. An important part of solving problems is searching for existing information about solving problems. If that searching process has a problem, then you can't solve the programming problem you started to search for. This may seem obvious or even trivial, but in the long run it's fundamental. That fundamental skill is learning how to learn.
     
  22. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    mikezang, this useful blog post should help you in the future: Getting Answers
     
  23. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #23
    Thanks.
     

Share This Page