PDA

View Full Version : How can I know which TableView?




mikezang
Jul 18, 2010, 07:36 AM
I have two TableView in a TableViewController as below, how can I know which TableView in cellForRowAtIndexPath?

-(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;
}



robbieduncan
Jul 18, 2010, 07:38 AM
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.

mikezang
Jul 18, 2010, 07:56 AM
I am sorry that I don"t what I can do, can you help me in more detail?

mikezang
Jul 18, 2010, 07:59 AM
I want to the top tableview only show one row for header, bottom tableview for detail rows and scrollable.

robbieduncan
Jul 18, 2010, 08:00 AM
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.

mikezang
Jul 18, 2010, 08:05 AM
Well, I have three class, CellsAppDelegate, CellsViewController and CustomCell, can you tell me where I can define outlet?

robbieduncan
Jul 18, 2010, 08:08 AM
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.

mikezang
Jul 18, 2010, 08:49 AM
Thanks for your advise, now I got what I need.

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

dejo
Jul 18, 2010, 11:01 AM
Why do you need two UITableViews?

mikezang
Jul 18, 2010, 11:06 AM
Why do you need two UITableViews?
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?

dejo
Jul 18, 2010, 11:12 AM
Why not just take advantage of UITableView's built-in header capability?

mikezang
Jul 18, 2010, 11:17 AM
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?

mikezang
Jul 18, 2010, 11:20 AM
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.

chown33
Jul 18, 2010, 11:42 AM
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.

mikezang
Jul 18, 2010, 12:18 PM
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.
Thanks! I never found a sample to use this property.

Where do I set the header? in viewDidLoad? or in -(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?

chown33
Jul 18, 2010, 12:56 PM
Thanks! I never found a sample to use this property.

Where do I set the header? in viewDidLoad? or in -(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?

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.

mikezang
Jul 18, 2010, 01:11 PM
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.

robbieduncan
Jul 18, 2010, 01:16 PM
Anyway, I have to thank your for your long text, that mean you are a nice guy, but not a good teacher.

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!

chown33
Jul 18, 2010, 01:21 PM
I googled "how to use uitableview tableheaderview to float", but I couldn't find useful information.

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.

mikezang
Jul 18, 2010, 01:36 PM
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.

chown33
Jul 18, 2010, 02:35 PM
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.

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.

dejo
Jul 19, 2010, 11:02 AM
mikezang, this useful blog post should help you in the future: Getting Answers (http://www.mikeash.com/getting_answers.html)

mikezang
Jul 19, 2010, 06:51 PM
mikezang, this useful blog post should help you in the future: Getting Answers (http://www.mikeash.com/getting_answers.html)

Thanks.