Multiple Details on UITableView - iphone

i've got a simple question. How to set up an UITableView with multiple details. I've got plist where i'm storing all the details.
This what I did for just one details.cell.textLabel.text = [[self.array objectAtIndex:indexPath.row] objectForKey:#"name"];
What to do if I want 2 details on my cell?
Many Thanks in advance.

First off you need to set the type of cell you want, if you want a title plus detail you can do this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
then for setting the title and detail labels, just
[cell textLabel].text = #"some title"
[cell detailTextLabel].text = #"some detail text"

you need to set style for the cell
have look into this guide

If your not happy with using cell.detailTextLabel and one of the provided styles(set up from storyboard) then one option would be to create a custom UITableViewCell.
You can either create a xib and drag and drop a cell in and customize it from there or if your only using 1 table it may be easier to use a prototype cell and customize it directly in your table. The key part is to create a new Class to support your cell.

Related

How to initialize a custom tableview cell

I am using storyboard and custom cells. I am trying to search. It works all fine and loads the custom cell when the tableviewcontroller loads , as soon as i enter a search character, the cell returned is UITableViewCellStyleDefault and i can only set the label, i am not sure why it is not picking the custom cell. Can someone please help.
sorry its a repeat of this but i could not figure out how that worked.
i am using this code.
static NSString *CellIdentifier = #"memberCell";
MemberCell *cell = (MemberCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *item = self.searchList[indexPath.row];
if (cell == nil) {
cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.memberFullName.text = [item[#"full_name"] uppercaseString] ;
cell.memberPosition.text = [item[#"title"] length] < 1 ? [NSString stringWithFormat:#"%#", item[#"districts"]] : [NSString stringWithFormat:#"%#, %#", item[#"title"], item[#"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:#"Rm %#", item[#"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[#"image_thumb"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
Deepak
You said "I am trying to search" - do you mean that you added a UISearchDisplayController and you are trying to get your custom cell to appear in that results table view? If so, you have 2 options:
In the tableView:cellForRowAtIndexPath: method you need to dequeue the custom cell from your main table view and NOT the search results table view. When you define a cell in tableview that is in a storyboard, the storyboard only registers it with that tableview, so it isn't dequeueable from any other tableview (I confirmed this with the apple engineer responsible for storyboards at WWDC '13). So, the code would look like this instead:
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Move your custom cell definition into a standalone xib file (create an empty XIB, drag a UITableViewCell from the objects list and configure it as desired). Define a custom cell class and configure that as the cell class in the xib, and then you can manually register that cell type with both table views using registerNib:forCellReuseIdentifier:

left+right aligned text in UITableView cell, breaks when table is scrolled

My goal is to display 2 strings in the same cell, one of them left aligned and the other right aligned. The code I have attached does just that in a table view, however it breaks when you scroll up/down. I need this to work in a table that can scroll. Someone had mentioned using CustomUITableViewCells instead of my current method, can anyone point me to an example of this?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20];
[rank setTag:5];
[cell.contentView addSubview:rank];
[rank release];
UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20];
[item setTextAlignment:UITextAlignmentRight];
[item setTag:6];
[cell.contentView addSubview:item];
[item release];
}
UILabel *rank = (UILabel *)[cell viewWithTag:5];
UILabel *item = (UILabel *)[cell viewWithTag:6];
rank.text = #"leftside";
item.text = #"rightside";
}
Any ideas and thoughts greatly appricated, thanks for lookin
This problem is because of dequeueReusableCellWithIdentifier. While the cell is being re-used, and when you scroll up and down it will cause major problems as labels are being added as subviews to the cells and they do not have the properties of the cell. However, if you use the cell.textLabel as your label, it would not cause problems like the one you are facing now, but you cannot add more than one label.
You have two solutions for this.
In your case, you need to stop using the same cellIdentifier for each and use different identifiers for each cells so that they do not get reused. This would be helpful if you have a very small number of rows in the tableView or it would turn out to be inefficient.
A better solution would be to subclass UITableViewCell and add those two labels in it's code, and then use that UITableViewCell with dequeueReusableCellWithIdentifier. This is just a small amount of work, and you get to re-use cells. This would be very helpful if you have a large number of rows in your tableview.
Go through THIS TUTORIAL to learn on how to subclass UITableViewCell with 2 labels.
You will need to work with the method, - (void)layoutSubviews and add those labels to your custom UITableViewCell subclass.
And remember to reference this customUITableViewCell instead of the default uitableviewcell when you are loading up the tableView. Your UILabels will not be messed up anymore.
Another reference.
Well, I have paste the same code that you have posted and I got 2 compiler error mentioning the ) is missing at the allocation of UILable and as I have clear it out, its compiled and started successfully.
The only exception and crash I have faced and that was due to the datasource method is not returning any cell. And that is also missing in given code.
Beside that, the code is working perfectly at my end and not having a single crash even though I scrolled many times.
So, just verify your code again or there should be another problem regarding datasource provided by array and also check the number of rows in section.
I would use a custom UITableViewCell. The easiest way is to just download a sample project, and copy and paste to see how you set up a custom cell. This is a good tutorial with a sample project included. You can use if (indexPath.row == int) in the cellForRowAtIndexPath method to determine which cells should be the standard ones, and which should be your custom cell.

A workaround to get textAlignment to center in UITableViewCellStyleSubtitle

According to this question (and many many google search results), when we're in a table view, and we set the cells to have UITableViewCellStyleSubtitle, we have been forced to align our texts inside the cell label to be on the left.
However I want one of the rows (in fact the row) of my table view to be aligned center... Obviously this wouldn't work:
cell.textLabel.textAlignment = UITextAlignmentCenter;
So I've been thinking what I should do instead... I'm not sure how to do it cleanly. The best way I can think of is to change this particular cell so that the style of the cell is UITableViewCellStyleDefault, but I don't think the cell property is changeable. Another way that I can think of is to subclass the UITableViewCell class, but that seems to be a bit of an overkill just for one table view cell. What do you think I should do to make that one last row aligned center?
EDIT: for those of you who're interested in the answer:
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifierDefault = #"CellDefault";
UITableViewCell *cell; // = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row >= [data count])
cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifierDefault];
else
cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil && indexPath.row < [data count]) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
else if(cell == nil && indexPath.row >= [data count]) {
//Use default style for the last cell.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierDefault] autorelease];
}
#the_great_monkey:
You can change the cellIdentifier for the cell where you want this center alignment.
For the other cells you should keep one common cellIdentifier.
Then when you do this
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
Then simply you need to check if cellIdentifier is the one for the cell which requires center alignment?
If Yes, then apply,
cell.textLabel.textAlignment = UITextAlignmentCenter; and cell.detailTextLabel.textAlignment = UITextAlignmentCenter; for that cell.
Hope this helps you.
It's easy to load a cell from a .xib, which lets you arrange the cell's subviews any way you like. Unfortunately, UITableViewCell's textLabel and detailTextLabel properties aren't IBOutlets, so you can't connect the labels that you'd like to use for these properties in IB.
One way to handle this is to connect those views to outlets in your controller instead, and use them when you set up the cell in -tableView:cellForRowAtIndexPath:. That's fine if you won't need to change the content of the cell later. In that case, you can just create a nib containing a UITableViewCell, set the type of File's Owner to match your controller, and set up the cell's subviews however you like and connect them to File's Owner. See Loading Custom TableView Cells from Nib Files for more information.
The other way to go is to create a UITableViewCell subclass that has outlets for the text label and detail label. Then do the same as above, but use your subclass rather than UITableViewCell. Once you've created this subclass, you can use it with different .xibs if you want different layouts at some point. The work needed to create the subclass is minimal, and it becomes a useful and reusable tool.
cell.textLabel.textAlignment=UITextBorderStyleLine;

How to add label and textfiled to each row in the UITableview

How to add label and textfiled to each row in the UITableview. can i add it through the Interface builder or have to do in the code,
Thanks,
Sam.
You can either make a custom table view cell class yourself, or you can use one of the built-in cell styles. Apple's "Settings" panel, for example, uses UITableViewCellStyleValue1.
Then you would set the detailTextLabel to the text, and add a sub view (your text field) to the contentView for your cell.
static NSString *CellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID] autorelease];
}
cell.detailTextLabel.text = #"a title";
[cell.contentView addSubview: xyz]; // xyz = your text field

How can i make my UITableViewCell contain two labels?

I want my UITableViewCell to look like the image below where there seems to be two labels. Is this possible without subclassing UITableViewCell?
alt text http://img31.imageshack.us/img31/2764/photoobp.jpg
There are different styles of UITableVieWCell. See here:
https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle
I think you want to use UITableViewCellStyleValue1.
You can initialise your UITableViewCell with the relevant style:
https://developer.apple.com/documentation/uikit/uitableviewcell/1623276-init
When you use a style that has two labels, you can use the textLabel and detailTextLabel properties to set them, respectively.
You do not need to subclass a UITableViewCell in order to add content to it. Here could be a sample cell generation method with an extra label:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *secondLabel = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
secondLabel.textAlignment = UITextAlignmentRight;
secondLabel.tag = 12345;
[cell.contentView addSubview:secondLabel];
}
UILabel *second = [cell viewWithTag:12345];
second.text = #"Second!";
return cell;
}
Let me know if you have any questions. I can clarify some things if needed.
Not sure where u think you see 2 labels...you can set the UILabels number of lines property if you want more lines UILabel ref....Also there is a UITableViewCell type UITableViewCellStyleSubtitle
which contains a detailTextLabel on top of the regular text labels in UITableCell, so you already have a built in cell with 2 text fields, here is a ref ref to UITableViewCell
Its not 2 labels but 2 buttons, you need to add 2 buttons in contentView view of the cell. Or you can create a footer or header View and add these 2 buttons.