I have a populated TableView where i would like to change the image. When I'm inside cellForRowAtIndexPath I can change it by using:
cell.imageView.image = [UIImage imageNamed:#"image.png"];
But I cannot figure out how to do the same thing inside the void, I have tried:
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]].imageView.image = [UIImage imageNamed:#"image.png"];
But it doesn't change the image at all.
Thanks,
/DSDeniso
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImage];
}
- (void)changeImage{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_row inSection:0]];
if(cell)
{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
} else {
NSLog( #"why is cell null? Did I set row properly?");
}
}
Just pass the cell to the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImageForCell:cell];
}
- (void)changeImageForCell:(UITableViewCell*)cell{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}
Related
I added button to table view cell, i want to select one button out of all when user tap on cell
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
_radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
_radioButton.frame = CGRectMake(30, 0, 30, 30);
[_radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[_radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
cell.accessoryView = _radioButton;
_radioButton.tag = indexPath.row;
}
if ([indexPath isEqual:selectedIndex])
{
_radioButton.selected = YES;
}
else
{
_radioButton.selected = NO;
}
cell.textLabel.text = [self.Option objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath;
[theTableView reloadData];
//I tried this also but not working
if (_radioButton.tag == indexPath.row) {
[_radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
}
}
I tried a lot. But it's not working.
I want to show selected image when user tap on cell. Either if we use button or image view
I got the solution like this.
NSString *selectedIndex;
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[theTableView deselectRowAtIndexPath:indexPath animated:NO];
if(indexPath.row != [selectedIndex intValue]) {
// reset the active account
selectedIndex = [#(indexPath.row) stringValue];
// tell the table to rebuild itself
[theTableView reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(indexPath.row == [selectedIndex intValue]) {
cell.imageView.image = [UIImage imageNamed:#"radio.png"];
}
else {
cell.imageView.image = [UIImage imageNamed:#"radio_empty.png"];
}
cell.textLabel.text = [self.Option objectAtIndex:indexPath.row];
return cell;
}
Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//custom code
[self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
//add table
....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"cell: %# -------- indexpath: %d", cell, indexPath.row);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
the problem is checkmarks are re-appear on another cells when scrolling.
Here's the result from NSLog:
cell: <UITableViewCell: 0x16f37a60; frame = (0 0; 320 50); text = ' Hoàng Anh'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 0
cell: <UITableViewCell: 0x16f37a60; frame = (0 450; 320 50); text = '84'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 9
cell: <UITableViewCell: 0x16f37a60; frame = (0 900; 320 50); text = 'Ân Thi'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 18
The same problem with cell 1 - 10 - 20 or 2 - 11 - 21 ...
As you can see, the address of cell doesn't changed. That's why the checkmarks re-appear.
How can I get rid of this issue ?
Thanks.
#property(nonatomic, strong)NSMutableDictionary *selectionDetails;
-(NSMutableDictionary)selectionDetails{
if(!_selectionDetails)
_selectionDetails = [[NSMutableDictionary alloc]init];
return _selectionDetails;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
**if([self.selectionDetails objectForKey:#"accessoryType"]){
NSString *status = [self.selectionDetails objectForKey:#"accessoryType"];
if([status isEqualToString:#"checked"])
[self.selectionDetails setObject:#"checked" forKey:#"accessoryType"];
else
[self.selectionDetails setObject:#"none" forKey:#"accessoryType"]
}else{
[self.selectionDetails setObject:#"none" forKey:#"accessoryType"]
}**
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"cell: %# -------- indexpath: %d", cell, indexPath.row);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
**[self.selectionDetails setObject:#"checked" forKey:#"accessoryType"];**
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
**[self.selectionDetails setObject:#"none" forKey:#"accessoryType"];**
}
}
Try this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *taxiDictionary = [self.arrayTaxiList objectAtIndex:indexPath.row];
if ([[taxiDictionary valueForKey:#"accessoryType"] isEqualToString:#"none"]) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
//a method that add context for cell, return void
[self cellView:cell withInfo:taxiDictionary];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"cell: %# -------- indexpath: %d", cell, indexPath.row);
NSMutableDictionary *taxiDictionary = [NSMutableDictionary dictionaryWithDictionary:[self.arrayTaxiList objectAtIndex:indexPath.row]];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
[taxiDictionary setValue:#"checked" forKey:#"accessoryType"];
} else {
[taxiDictionary setValue:#"none" forKey:#"accessoryType"]
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
sorry guys, the problem which I was described happens because of my stupidity.
It's all happen in this line of code
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(void)cellView:(UITableViewCell *)cell withInfo:(NSDictionary *)info {}
this method receives an UITableViewCell and edits its context. So, that's why the NSLog above returns an UITableViewCell with the same address in memory.
I changed it to
cell = [self cellViewWithInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(UITableViewCell *)cellViewWithInfo:(NSDictionary *)info {}
and now it works like it should've to.
Thank you guys for replying.
I want to design a tableview such that each cell has a different background image depending on a condition. There are 10 images for cell background and I want to load them as such that the image pattern is repeated on the cells. For example, cells 1-10 take images from 1-10 respectively. Then cells 11-20 also take images 1-10 and so on. How can I achieve this?
give image name like: images_1/2.../10.png
//write it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell1";
CustomCell *Cell = (CustomCell *)[tabeleYourTurn dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
NSArray *topLevelObject= [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
}
for (id currentobject in topLevelObject)
{
if ([currentobject isKindOfClass:[UITableViewCell class]])
{
Cell = (CustomCell *) currentobject;
break;
}
}
}
NSString *image = [NSString stringWithFormat:#"images_%d.png", indexPath.row % 10 + 1];
Cell.imageView.image = [UIImage imageNamed:image];
return Cell;
}
May be it will work.
First create an array with ImageName..
Like
NSArray *ImageArray=[[NSArray alloc]initWithObjects:#"1.png",#"2.png"];
Then this array with 10 images
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// create a background image for the cell:
UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundView:bgView];
[cell setIndentationWidth:0.0];
((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:[ImageArray objectAtIndex:indexPath.row%10]];
}
Assuming that your image names are 1, 2...10, you can try to do something like this:
NSString *imageName = [NSString stringWithFormat:#"%d", indexPath.row % 10 + 1];
Than you can use this imageName where you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if( (indexPath.row % 10 ) <= 1)
{
// set your image
cell.imageView.image = [UIImage imageNamed:#"image1.png"];
}
}
- (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];
}
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
this is my code when ever i started scrolling the table towards downwards or upwards the first cell which i have checked have loosed the check mark sign, it is just hides all the way.. what is wrong with my code?
Reuse Cell ,
- (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];
int getIndex = [sharedGameState.selectedValues indexOfObject:[NSNumber numberWithInt:indexPath.row]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if(getIndex<21)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
} // END HERE
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
For selecting multiple rows:
- (void)addTableView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.timeSheetEntryTableView.allowsMultipleSelection = YES;
self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];
}
//Cell for row at index path
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"Cell Identifier";
self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!self.cell) {
self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
self.cell.accessoryType = UITableViewCellAccessoryNone;
}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box_selected.png"]];
}
else
{
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box.png"]];
}
return self.cell;
}
//did select and deselect
// I have used custom cell
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box_selected.png"]];
}
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array removeObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:#"check_box.png"]];
}
I have data and image in cells of UITableView. I want to change image of a particular cell,
I have a code with each cell.
I am not able to find the way to iterate the tableview for cell.
here is snap shot of my tableview function.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = nil;
NSArray *keys = [[[self packageItems] allKeys]
sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
cellText = [keys objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
//cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
return cell;
}
There is another function where i want to change cell image to cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
- (void)OnAction
{
static NSString *CellIdentifier = #"Cell";
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath ];
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
}
If you wont to change something in table view with you function you dont have to make new cell, just modify actual cell. Just make condition in your cellForRowAtIndexPath delegate:
if(indexPath == indexToModify) {
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
} else {
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
}
and in your - (void)OnAction method use in the end:
[self.tableView reloadData];