Why is my ImageView only visible when the cell is highlighted? - iphone

I have a custom Cell for my TableView, with an ImageView on it.
Why is the ImageView only visible when the cell is highlighted?
The cells are created thusly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
Site *site = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:site.description];
return cell;
}

Check out: Custom UITableViewCell (IB) only shows in selected state
The quick synopsis of it "Filling the standard UITableViewCell.textLabel.text seems to overwrite the PrototypeCells". So it would appear you have to create and add a different text label.

Maybe your UIImageView is a subview of the highlighted background view, only visible when the cell is selected?

Related

Prototype cells different from dynamically created cells

I have created two types of prototype cells in storyboard. The dimension of one of them have been customized to accomodate UIButton object. However when the cells are created, they have the standard height. I can see the UIButton object but it gets truncated because of the cell height.
Why are the newly created cells different from the prototype cells?
The relevant section of the code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(cell == nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"PictureSelectionCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
pictureButtonProperty = (UIButton *) [cell viewWithTag:1];
}
}
Going forward, what are my options for creating the cell of the width (or dimensions) defined in the storyboard? Programmatically, I will be able to achieve this by creating a CGRect object with the specified dimensions and then create a cell using initWithFrame. However, I would like to avoid doing things manually.
Thanks for your response.
first of all you can always set it with code
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YOUR_ROW_HEIGHT;
}
other way if you choose your UITableView if the storyboard, under the size inspector change the Row Height.

How to access the cell in tableview?

I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.

Values are getting cleared from UITableViewCell while scrolling

I have placed three buttons over UITableViewCell and its background is set to an unselect radio button image.
While clicking the button I'm again setting the background image as select button.
In a row one select button should persist but while scrolling UITableView all the selected button images are getting cleared
Can somebody please give me any idea how to do this in this way or in other way ?
You probably are not reusing the cells correctly. When you scroll, a previous cell is reused by the UITableView, but you don't change its contents correctly. Here is a sample code for ur reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [urDataArray objectAtIndex:indexPath.row];
return cell;
}
save the state of you button in table datasource.
set that value to your button. table is reloading each displayed row while scrolling.

Hierarchical UITableview

In my first view I have a UITableView with 5 records. In UITableView cells are holding two different components. One UILabel is text and another UILabel shows value for that cell.
On selection of this UITableViewCell user is navigated to another view where I have given a picker controller for changing the value of 2nd label in the table view for the selected row. I reload the UITableView data on -viewWillAppear but this overlaps the text on lables as many times I visit it.
Can anyone give me relevant example of hierarchical tableview where UItableViewCell's right section is dependant on selection of picker from next view.
I think you are adding the labels on cell.contentView everytime you reload the table, but are not removing the previous ones. Thats why the overlapping is happening.
try to remove the lables
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for (UIView * view in cell.contentView.subviews) {
[view removeFromSuperview];
view = nil;
}
// add your labels
}

images in tableview cells

Ive added a uiimageview as a subview to my cell.and then put a label on top of it so that it would seem like a button.But whwn the table is scrolled up or down the image seems to get painted again.This turns extremely ugly as my image has transparency effect which is lost as soon as it goes out of view and comes back.???
Ok, I'll try to guess what your code looks like :)
If images are painted multiple times that means you add them each time table view queries data source for a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView #"SomeID"];
if (cell == nil) {
// Create cell
}
UIImageView *imView = ... //Create and initialize view
[cell.contentView addSubview:imView];
...
return cell;
}
So each time your cell appears on screen (after user scrolls the table) new instance of image view is added to the cell. The correct approach is to add image view only once - when cell is created and then obtain and setup the existing image view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView #"SomeID"];
if (cell == nil) {
// Create cell
UIImageView *imView = ... //Create and initialize view
imView.tag = 1000; // or any other int value
[cell.contentView addSubview:imView];
}
UIImageView *iView = (UIImageView *)[cell.contentView viewWithTag:1000];
iView.image = ...// set required image
...
return cell;
}
So with this approach each time the cell is reused by table view the existing image view is populated with the image appropriate for the current row.
I used a seperate identifier for each cell.
Usually it is not a good idea - in this case table won't be able to reuse its cells and you may get serious performance troubles