UIImageView simply does not appear in custom UITableViewCell - iphone

I have a table cell to which I'm adding subviews programmatically. All the textual subviews work fine, but I can't get an image subview working at all.
You'll notice that I set the background color to black. This is simply to indicate to me that the subview is indeed being initialized and positioned properly within the cell. When I remove the background color there is nothing there.
Also, the cell style is UITableViewCellStyleDefault but I don't think that's pertinent for custom subviews. I want the image positioned on the right, which is why I'm not using the standard imageView property that cells offer.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// ... add textual views ...
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"clock.png"]];
img.frame = CGRectMake(271.0f, 10.0f, 19.0f, 22.0f);
img.backgroundColor = [UIColor blackColor];
[cell addSubview:img];
}
// ... more code ...
return cell;
}

Your code looks fine to an extent. One issue I had when I was creating my own UITableViewCells in code was I was creating the cells with variables like you are but you shouldn't do this.
What you should do is create the cell and then set the variables. If cells are different you should use different reuse identifiers. Hope this makes sense if not let me know and I'll update.

Related

Issue with UIImageView z-index

In the interface designer, I have a very simple setup. The main view (which is a table cell view) has four sub-views, all siblings: UIImageView and three UILabels. I'm using UIImageView to display the background of the table row. I set it so that UIImageView is at the very back of the z-index tree and set its alpha to 0.2 to dim the image - and UILabel's are going over it.
If I set the image into the imageview at the design time in interface designer, then everything is fine, however if I set the image into the imageview at design time (I receive the actual image from the server), then the imageview alpha setting is ignored, the image is displayed at alpha = 1 and the uiimageview is displayed over all the labels. I even tried to use [parent sendSubviewToBack:imageView] but it didn't help.
What am I missing? Here's my whole code for displaying the table row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"OOItemInfoCell" owner:self options:nil] lastObject];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
OOXMLNode *infoNode = [searchItemList objectAtIndex:indexPath.row];
[(UILabel *)[cell viewWithTag:2] setText:[itemNode elementNodeWithKey:#"title"].value];
[(UILabel *)[cell viewWithTag:3] setText:[itemNode elementNodeWithKey:#"intro"].value];
[(UILabel *)[cell viewWithTag:4] setText:[NSString stringWithFormat:kThemeAge,
[itemNode elementNodeWithKey:#"theme"].value,
[itemNode elementNodeWithKey:#"age"].value]];
UIImageView *bgview = (UIImageView *)[cell viewWithTag:0];
[bgview setImage:[itemNode imageFromKey:#"background"]];
[cell sendSubviewToBack:bgview];
return cell;
}
Ok, this was pretty stupid of me... I had the tag of the UIImageView set to 0 and then was using viewWithTag:0 to find that view. Yet, the parent view also had tag 0, so instead of my background UIImageView, the entire cell was being set to the image (I wouldn't think that a table cell view would respond to setImage - but it did.
I now changed the tag on UIImageView to 5 - and everything is hunky-dory.

How to place a Rounded Rect UIBUTTON on UITableViewCell programmatically

I was trying to place two uibuttons on uitableviewcell programmatically.. but unable to achieve that>. somehow i managed to get the buttons through some sample project in which u can draw a nib for cell.. but i was unable to set a selector function for buttons.. in other example i can place a uiinfodark type button but when i change the type to rounded rect the button gets disappeared and i am unable to figure out why? I might be doing some silly mistake but I am really kinda stuck with it right now so I'll be very much thankful to all of you for helping in this regard.
You will need a UITableViewDataSource
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Then something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Get the object to display and set the value in the cell.
[cell addSubView: YOUR_BUTTON];
[cell addSubView: YOUR_OTHER_BUTTON];
return cell;
}

Custom drawing in UITableView failing after 5 cells

I'm having a funky problem with drawing a custom view in a UITableView. The drawRect function of my custom view, called ChatBubbleView, is just drawing a rectangle of a constant size.
I have an NSLog debugging statement in the drawRect function of ChatBubbleView. The first 5 times I add a cell to the UITableView, everything draws nicely, and I see the NSLog statement trigger. After 5 however, the drawing becomes garbled, and I no longer see the debugging statement.
I'm totally baffled, and I'd appreciate any help you might provide. Thanks in advance!
Here is the function that is called when reloadData is called on the UITableView.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (forumMode) {
//This part works, ignore
} else {
ChatBubbleView* chatBubble = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
// Add the bubble
chatBubble = [[[ChatBubbleView alloc] init] autorelease];
chatBubble.tag = BUBBLEVIEW_TAG;
[cell.contentView addSubview:chatBubble];
} else {
// Reuse old views
chatBubble = (ChatBubbleView*)[cell.contentView viewWithTag: BUBBLEVIEW_TAG];
}
chatBubble.labelSize = CGSizeMake(50, 18);
chatBubble.frame = CGRectMake(0, 0, chatBubble.labelSize.width, chatBubble.labelSize.height);
}
return cell;
}
EDIT: I should add that the function that reloads the UITableView looks like this:
-(IBAction) btnAdd:(id) sender {
[listOfMessages addObject:textView.text];
[self.tableView reloadData];
}
EDIT 2: I've discovered that the custom views in the cells sometimes redraw themselves when I scroll the cells in and out of view. It seems that the tableview refreshing changes the parameters of the custom view.
You say //This part works, ignore but if you are reusing the CellIdentifier it might not work as the cell that will get dequeued could be a different type of cell (that happens to have the same CellIdentifier). Make sure each cell type has a unique CellIdentifier.
To be sure, put some debugging in your if / else block to see what cell you're getting back...

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.

Cocoa-Touch: How to: custom UITableViewCell with UIColor clearColor background

I have a UITableView with custom cells.
I have MyTableViewCell : UITableViewCell and MyTableViewCellContentView : UIView classes.
What I'm doing is basically what is done in the AdvancedTableViewCells demo app from Apple with a slight change, on some rows I want to use a clearColor background to show the table's background behind the painted text.
So in MyTableView's cellForRowAtIndexPath I'm doing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.someValue = indexPath.section;
[cell finishedSetup];
return cell;
}
And in my MyTableViewCell's finishedSetup:
cellContentView = [[MyTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 0, 320, 80) cell:self];
cellContentView.autoresizingMask = UIViewAutoresizingNone;
cellContentView.contentMode = UIViewContentModeRedraw;
[self.contentView addSubview:cellContentView];
And in MyTableViewCellContentView I implement the drawRect method. And plan to not use any subviews but draw my custom content just as the Apple example does in the CompositeSubviewBasedApplication.
The problem is that for a few sections I want to use a clearColor backgroundColor. This works, until a cell with a non-clearColor backgroundColor is reused to draw a clearColor cell, at which time the background is not cleared and will still have the old color.
How can I make the background redraw?
Edit:
I forgot to mention, I'm setting the background color in MyTableViewCellContentView's init after calling super's init. Setting it via:
self.backgroundColor = [UIColor clearColor];
I've verified that this in fact does get called and is called as expected with clearColor or redColor.
I've also tried setting the table cell's background color, it didn't help.
Edit #2: Here's my drawRect method:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
static int i = 0;
[[NSString stringWithFormat:#"Cell %d", ++i] drawAtPoint:CGPointMake(3, 3) withFont:[UIFont boldSystemFontOfSize:16]];
}
To make the background color setting take effect you need to do the setting in tableView:willDisplayCell:forRowAtIndexPath - the OS will not alter anything you set here. The reason is that some additional setup of the cell gets done by the OS after you return it from tableView:cellForRowAtIndexPath. If you can, get a look at session 101 from WWDC 09 presented by Jason Beaver.
I haven't found out why it was happening. But I've tried a different strategy. I'm no longer reusing table cells, since I don't have that many of them.
Instead I'm creating all cells at startup and actually get a boost in performance when showing them since this way there is no additional setup needed when scrolling.