Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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;
}
 

Attachments

  • SnapShot 2010-07-18 at 21.34.34.jpg
    SnapShot 2010-07-18 at 21.34.34.jpg
    43 KB · Views: 78

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
I am sorry that I don"t what I can do, can you help me in more detail?
 

Attachments

  • Archive.zip
    24.2 KB · Views: 49

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
I want to the top tableview only show one row for header, bottom tableview for detail rows and scrollable.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
Well, I have three class, CellsAppDelegate, CellsViewController and CustomCell, can you tell me where I can define outlet?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
Thanks for your advise, now I got what I need.

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

Attachments

  • SnapShot 2010-07-18 at 22.48.03.jpg
    SnapShot 2010-07-18 at 22.48.03.jpg
    48.6 KB · Views: 83

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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?
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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.
 

Attachments

  • SnapShot 2010-07-19 at 1.21.17.jpg
    SnapShot 2010-07-19 at 1.21.17.jpg
    76.8 KB · Views: 68
  • SnapShot 2010-07-19 at 1.21.33.jpg
    SnapShot 2010-07-19 at 1.21.33.jpg
    74.8 KB · Views: 87

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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?

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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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

macrumors 6502a
Original poster
May 22, 2010
861
9
Tokyo, Japan
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

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.