Where do I set backgroundImage of tableview cell in cellForRowAt.. And how do I do that ? This is how I did it and it kinda works, but I want to know is that right method for setting backgroundimage (cell.backgroundView) and is it right place inside if(cell==nil) or outside that "if".?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
Used to do that, but nowadays I prefer willDisplayCell:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
Documentation says
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.
When using cellForRowAtIndexPath, there were some weird special cases, where cell customization just didn't work fully as I wanted. No such problems with willDisplayCell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.imageview setImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
try with this might be helpful...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIImageView *backimg = [[UIImageView alloc] init];
backimg.frame = CGRectMake(0, 0, 320, 44);
backimg.tag = 20;
[cell.contentView addSubview:backimg];
}
UIImageView *imag = (UIImageView *)[cell.contentView viewWithTag:20];
[imag setImage:[UIImage imageNamed:#"sky.png"]];
return cell;
}
The best place to set a background view for a cell is the tableView:willDisplayCell:forRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}
Related
I want the whole cell printed in blue but it only shows a little band.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
My screen capture is this:
add this line after [cell.contentView setBackgroundColor:[UIColor blueColor]]; line:
cell.textLabel.backgroundColor = [UIColor clearColor];
Because of the way UITableView changes a cell's background color during selection, you must implement tableView:willDisplayCell:forRowAtIndexPath: and set the cell background there. You should set the background of the whole cell rather than just its contentView, otherwise the accessory views will not be highlighted.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
cell.backgroundColor = ...;
}
You may also need to make the various subviews in the cell have transparent background colors.
cell.titleLabel.backgroundColor = [UIColor clearColor];
cell.titleLabel.opaque = NO;
Use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
Answer : tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor blueColor];
}
I have a custom cell with imageview and label.When user selected a particular cell in the tableview I want change its image and the label font color.How can I do this ?I want to avoid that default way of cell selection(highlight cell in blue color) and use above method instead of that.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *selectedCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
//selectedCell.textLabel.textColor = <#your color#>
//selectedCell.myImage = <#your image>
}
if you want changed selected cell backgroundColor, try to this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
}
}
in my tableview i use a background image for my cells (totally black image) and i also want to use accessory indicator in my cells. in cell's properties window i set AccessoryDisclosureIndicator
in my .m file i put these codes for tableview:
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryDisclosureIndicator;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
}
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[arrMagaza objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
NSString *distance=[arrDistance objectAtIndex:indexPath.row];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.text=[distance stringByAppendingFormat:#" km"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tablecell.png"]];
tableView.backgroundColor=[UIColor blackColor];
tableView.separatorColor= [UIColor clearColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
but the accessory indicator not shown on the view when i run the app.
what am i doing wrong? is the problem about background image that i use for my cells?
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
This method is deprecated now, so no need to implement this method. Try removing everything else looks fine.
i ve got an imageview ..below the imageview is a table view..which loads the data from server.by default five fields in table view are visible ..works fine..if i m to scroll the table view to view the remaining fields..the scroller rebounces..wat i want is the table view to be fixed and not to rebounce when scrolled ..i d be so greatful if u guys cud help me out..below is the code..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 28.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,
tableViewteam.bounds.size.width, 28)] autorelease];
headerView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"team"ofType:#"png"]]];
return headerView;
}
- (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];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.imageView.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[[items objectAtIndex:indexPath.row]objectForKey:#"title"];
cell.textLabel.textColor=[UIColor whiteColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I didn't fully understand your problem. If you want to stop bouncing the table view you can add
yourTableView.bounces = NO;
in ViewDidLoad()
If you need your tableView to be fixed you can disable scrolling.
tableView.scrollEnabled = NO;
I'm trying to change the custom accessoryView of a uitableviewcell immediately after the user clicks on the cell. How would I do this?
For the record, I'm using Matt Gallagher' custom table view tutorial:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
Download link for source: http://projectswithlove.com/projects/EasyCustomTable.zip
EDIT
Tried this code, but it just changes the accessory AFTER touch-up. Also tried willSelectRowAtIndexPath with same result.
- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *indicatorImage = [UIImage imageNamed:#"indicatorSelected.png"];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
return indexPath;
}
EDIT
Problem solved by making the background image of the cell contain the accessories. so the accessory was 'faked' by making it part of the image
Perhaps you can try to reload the cell in willSelectRowAtIndexPath/didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *indicatorImage = [UIImage imageNamed:#"indicatorSelected.png"];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:NO];
}
Another recommendation:
didSelectRowAtIndexPath returns void not NSIndexPath.
You could change the accessoryView in the selection method as shown:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = newView;
}
This just grabs the current cell that was selected and then changes the accessory view of it. Your view object would go where newView is.
Use the highlightedImage property of UIImageView:
UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];