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;
}
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 am using Coredata and NSFetchedResultsController to store and retrieve Values and show them in a table view.I am creating a custom label in cellForRowAtIndexPath and displaying the value of an attribute 'lastname'. But I am getting wrong values.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label = nil;
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
//Configure the cell
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
label.text = list.lastname;
}
[self configureCell:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
the wierd part is that it is working fine if i remove the UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; line and the if condition.
You need to move
label.text = list.lastname;
outside of the
if(cell == nil)
The reason is, that the content inside of the if(cell == nil) will get called only x times, where x is the number of visible cells on the screen. As you scroll, the new cells are being reused and that's why they contain some incorrect value.
EDIT:
You will also need to move your
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
outside of the if.
EDIT 2:
This is how it should look like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label = nil;
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease];
label.backgroundColor = [UIColor clearColor];
label.tag=1;
[cell.contentView addSubview:label];
}
[self configureCell:cell atIndexPath:indexPath];
//Configure the cell
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
label = (UILabel*)[cell.contentView viewWithTag:1];
label.text = list.lastname;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
this should work for you
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;
}
In my cellForRow code I have the following and am getting an error saying nothing is being returned. What have I done wrong? Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
This is an endless loop: UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
This should be:
static NSString *reuseIdentifier = #"some_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]
}
Your cellForRowAtIndexPath will look something like this (for the most Basic Table View)
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex :row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}
I m parsing json data and assigning it in my table view cell.where in the first row of tableview cell .i m assigning a static data(green text).if i scroll my tableview the data assigned in first table view cell is getting overlaped in my 6th and 7th tableview cell.below is the screen shotand the code.could u guys help me out
- (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];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
return cell;
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[story objectAtIndex:indexPath.row];
return cell;
}
}
I think it's because the cell is reused. Try this code instead:
- (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];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
cell.accessoryType=UITableViewCellAccessoryNone;
return cell;
}
else
{
cell.textLabel.text=[story objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=nil;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
Just make the else part as follows.
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[NSString stringWithFormat:#"Cell %d",indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=#"";
return cell;
}