I used willDisplayCell delegate method to show a custom background image to a UITableViewCell.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
static UIImage* bgImage = nil;
if (bgImage == nil) {
bgImage = [UIImage imageNamed:#"myImage.png"];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
}
When I run the app in the simulator, the background only for the cells that are drawn are changed, is there a way to change all cells' background?
I have worked this code.It's working good .you can try this code.
- (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 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
UIImageView *cellBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
cellBackView.backgroundColor=[UIColor clearColor];
cellBackView.image = [UIImage imageNamed:#"list.png"];
cell.backgroundView = cellBackView;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
You have to set the backgroundView property in the cellForRowAtIndexPath method that creates the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:...];
return cell;
}
Related
*My code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ExploreTBLCellID";
ExploreTBLCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ExploreTBLCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"new one");
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSDictionary *distGetData;
distGetData = [arrget_smart_category objectAtIndex:indexPath.row];
[imgMain sd_setImageWithURL:[NSURL URLWithString:[distGetData objectForKey:#"image"]]
placeholderImage:imgPlaceholderExlore];
return cell;
}
I added a UIImageview as a subview of UITableViewCell.contentview. It has margins in left and right side of the cell. when rotating device, Autoresizing is not working for me. I am using:
UIImageView *imgviewcellBg=[[UIImageView alloc]initWithFrame:CGRectMake(9, 3, self.view.bounds.size.width-18, 33)];
imgviewcellBg.autoresizesSubviews=UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:imgviewcellBg];
Help me. Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]autorelease];
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, tblMyWines.frame.size.width, cell.frame.size.height);
}
UIImageView *imgviewcellBg=[[UIImageView alloc]initWithFrame:CGRectMake(tbl.frame.origin.x+5, 3, tbl.frame.size.width-10.0, 33)];
[cell.contentView addSubview:imgviewcellBg];
return cell;
}
I have an UITableViewCell and I add a subView to a label.
[cell addSubview:stateLabel];
I want to change the label when the user presses a cell.
[tableView indexPathForSelectedRow];
This gives me the pressed indexPath, but can anyone help me how to change the label at that position where the user pressed?
I think I just need a hint and then I can do the rest by myself...
For example, if the user presses cell 8, it changes the stateLabelsubview of cell 8.
This is what I've got so far:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellID";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
stateLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(240,0,100,44)];
stateLabel.textColor = [UIColor redColor];
stateLabel.backgroundColor = [UIColor clearColor];
stateLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(13.0)];
stateLabel.text = #"Not applied";
[cell addSubview:stateLabel];
}
cell.textLabel.text = [cheatNames objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *selectedRowPath = [tableView indexPathForSelectedRow];
}
Thanks!
you can implement something like this
in your cellForRowatIndexPath: Method before the return cell;
get the indexpath of the selected row for changing the label.
and use
if(indexpath == selected index)
{
// do ur code here
}
as soon as didSelectRow: is called reload the table
this will defintely work.
no reload required IMHO:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *selectedRowPath = [tableView indexPathForSelectedRow];
myString = #"Any Data";
[tableView reloadData];
UITableCellView *cell = [tableView cellAtIndexPath:indexPath];
cell.label.title = myString;
}
Define myString in the .h file as an NSString
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *selectedRowPath = [tableView indexPathForSelectedRow];
myString = #"Any Data";
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellID";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
stateLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(240,0,100,44)];
stateLabel.textColor = [UIColor redColor];
stateLabel.backgroundColor = [UIColor clearColor];
stateLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(13.0)];
stateLabel.text = myString;
[cell addSubview:stateLabel];
}
cell.textLabel.text = [cheatNames objectAtIndex:indexPath.row];
return cell;
}
I am trying to implement UITableView based Application.For that I select UITableViewStyle is Group.In my TableView their is 15 section each section having 1 row.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 15;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==12)
{
return 120;
}
else
{
return 60;
}
}
I want add a UITextView on Section 12
For that i did following code
- (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];
}
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
descriptionTextField.font = [UIFont systemFontOfSize:15.0];
descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];
[descriptionTextField setDelegate:self];
[descriptionTextField setTag:2];
[descriptionTextField setText:#"Enter Location Description."];
descriptionTextField.keyboardType=UIKeyboardTypeDefault;
descriptionTextField.returnKeyType=UIReturnKeyNext;
descriptionTextField.textColor=[UIColor blackColor];
descriptionTextField.editable=YES;
descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
descriptionTextField.textAlignment=UITextAlignmentLeft;
UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];
UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(keyboardDoneButtonActin:)];
[Done setWidth:65.0f];
[keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
[extra release];
[Done release];
[keboardToolBar setTintColor:[UIColor blackColor]];
[keboardToolBar setAlpha:.70];
[descriptionTextField setInputAccessoryView:keboardToolBar];
[descriptionTextField setTag:101];
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
In the initil stage the table view like this
if i am scrolling tableview up and down , then the uitextView section changed and it will display multiple location.
I can't understand my fault, why this happend?
if am implementing the above code in side the if(cell==nil)
- (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];
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
**/* implemention all code here*/**
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
UITextView not disply, i think it is not allocating.
so what is the difference between code implementing in
if (cell == nil)
{
inside
}
if (cell == nil)
{
}
out side
The test if (cell == nil) handles the case where there are no reusable cells to dequeue, in which case you must create a new cell. When you create the new cell, you are responsible for constructing its view hierarchy.
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This can be used instead of
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
this is the simple example to write inside the cell ==nill
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
cell= nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview];
UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
[selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"strip_s12A-1_h.png"]]];
cell.selectedBackgroundView = selectview;
[selectview release];
UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
//cellTitle.adjustsFontSizeToFitWidth=YES;
[cellTitle setBackgroundColor:[UIColor clearColor]];
[cellTitle setFont:[UIFont fontWithName:#"Arial Rounded MT Bold" size:17]];
[cellTitle setTextColor:[UIColor blackColor]];
cellTitle.tag = 100+indexPath.row;
cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:#"Des2"];
[cell.contentView addSubview:cellTitle];
[cellTitle release];
}
return cell;
}
i think will be enough right
I am trying to add a pickerView to a row in my table view, but after doing this, all I see is a black rectangle inside the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200, 300)] autorelease];
[cell.contentView addSubview:pickerView];
}
}
I really don't have a clue how to solve this.
Can anyone help me, please?
May be this can help: DateCell with TableViewController