I'm trying to customize how my UITableView looks by setting the backgroundView with an UIImageView, but is not working properly as you can see in the image. (only works for the accessoryType and at the start of the row before the text.)
Can anybody help me with this?
My code:
// 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell.
NSDictionary *lineaAutobus = [buscamionService objectAtIndex:indexPath.row];
NSString *lineaAutobusNombre = [lineaAutobus objectForKey:#"nombre_lineaAutobus"];
[cell setBackgroundView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tableViewCellBg.png"]] autorelease]];
cell.textLabel.text = lineaAutobusNombre;
return cell;
}
https://www.dropbox.com/s/ubok76ipshei931/Screen%20shot%202011-05-20%20at%206.25.29%20PM.png
Perhaps you need to set the backgroundColor of the textLabel to [UIColor clearColor]?
Related
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];
}
}
I am using a grouped table view for my iPhone application, for which I need to change the selected background color to red. I can set it, but the problem is that the first table cell the views are outside the table cell.
try to this....
- (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.backgroundColor = [UIColor clearColor]; // clear the cell background color
// Configure the cell.
return cell;
}
You can use following code
1)cell.selectionStyle = UITableViewCellSelectionStyleRed;
OR
2)cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
use any of above code will solve your problem if still your problem is not solved put your code i'll try to solve
I have created a TableView(Grouped Table View). And I can list the items in the table view. But I want to know how the below table is displayed
Like Calender is in the left side, and Gregorian in the Right Side ?? How that Gregorian is displayed in the Right Side ?? Is it a Custom TableView, Can anyone help me how to achieve that ?
Those are just regular cells with style = UITableViewCellStyleValue1 and also accessoryType = UITableViewCellAccessoryDisclosureIndicator.
To make one programmatically:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"YourIdentifier"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
(Note that in real life you would probably try to dequeueReusableCellWithIdentifier:, and only create a new cell if that returns nil.)
Or, if you're working with IB, set "Style" to "Right Detail" and "Accessory" to "Disclosure Indicator".
You need to use following code helping to you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIImageView *v = [[[UIImageView alloc] init]autorelease];
v.backgroundColor = [UIColor clearColor]; // put clear color
[v setClipsToBounds:YES];
cell.selectedBackgroundView = v;
[cell setClipsToBounds:YES];
}
UIImageView *imgVBG=(UIImageView*)[cell selectedBackgroundView];
if(indexPath.row==0){
imgVBG.image=[UIImage imageNamed:#"TopImg.png"];
} else if((indexPath.section==0 && indexPath.row==4)||(indexPath.section==1 && indexPath.row==7)){
imgVBG.image=[UIImage imageNamed:#"BottomImg.png"];
} else {
imgVBG.image=[UIImage imageNamed:#"MiddleImg.png"];
}
}
Here is your answer UITableViewCell with UITableViewCellStyleValue1, adding new line to detailTextLabel at cell at bottom
My application navigation based and view is UITableviewController. I need to add switch control for ON and OFF for particular UIViewtable cell. How to add.
Also a classic, this question has been asked a lot here already. For example, see this question. Search for UITableView and UISwitch to get way more results.
You can add a switch in the -tableView:cellForRowAtIndexPath: method found in the UITableViewDataSource Protocol
- (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];
}
// Configure the cell...
UISwitch *aSwitch = [[[UISwitch alloc] init] autorelease];
[aSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
return cell;
}
How to show a detail disclosure button to the uitableview without using the custom cell. I know its by using the accessoryType: method, but don't know how to implement that..pls help..
// 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];
}
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
}
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;