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];
Related
I making the tutorial on running iPhone 5S, Xcode 6 and iOS 8. I want to display the textDetailLabel in a cell table. Can you help me what's the problem here. I already checked the syntax but it doesn't work
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Checklist *checklist = [self.dataModel.lists objectAtIndex:indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
cell.detailTextLabel.text = #"(No Items)";
} else if (count == 0) {
cell.detailTextLabel.text = #"All Done!";
} else {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d Remaining", count];
}
return cell;
}
Move
static NSString *CellIdentifier = #"Cell";
out of the method, make it public.
When you create the table view write this line where you create the table (in viewDidLoad or your method where you create the table)
[dataTable registerClass:[UITableViewCell class] forCellReuseIdentifier:tableIdentifier];
Remove cell condition from
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
method because your table view cell is not nil and you can't set the cell style.
So you have to write
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
What i am trying to do is parse an XML and have the elements populate the table view. The XML parses fine and prints in console, but is unable to display in the table view. Here is my code for populating a cell:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:#"title"]];
[cell.detailTextLabel setText:[news objectForKey:#"link"]];
[cell.detailTextLabel setNumberOfLines:2];
}
return cell;
}
I got it to display. Seems I forgot to set the data source in the viewDidLoad method... thank you everyone for all the help.
You are only adding data to the text labels when you create a new one. Add the data to the view after the if statement.
UITableView will reuse cells when possible. When it does you will get an existing cell from dequeueReusableCellWithIdentifier:.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
NSMutableDictionary *news = (NSMutableDictionary*) [feeds objectAtIndex:indexPath.row];
cell.textLabel.text = [news objectForKey:#"title"];
cell.detailTextLabel.text = [news objectForKey:#"link"];
cell.detailTextLabel.numberOfLines = 2;
return cell;
}
Fill the data After creating creating or reusing your cell . It will work fine now ,Use below code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:#"title"]];
[cell.detailTextLabel setText:[news objectForKey:#"link"]];
[cell.detailTextLabel setNumberOfLines:2];
return cell;
}
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"];
}
}
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"];
}
My tableview works perfectly fine when not in editing mode. All cells show up as expected, but if I enter editing mode and scroll, the cells that are redrawn while in editing mode have the incorrect content. In my function that turns off editing, I reload the table data and it shows up correctly again.
Here the relevant code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
FieldItemDecrypted *theField = [decryptedArray objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [[NSString alloc] initWithData:theField.field encoding:NSUTF8StringEncoding];
cell.detailTextLabel.text = [[NSString alloc] initWithData:theField.type encoding:NSUTF8StringEncoding];
return cell;
}
And my code for editing:
- (IBAction)editRows:(id)sender
{
if ([self.tableView isEditing])
{
[self.tableView setEditing:NO animated:YES];
[self.tableView reloadData];
}
else
{
[self.tableView setEditing:YES animated:YES];
}
}
Should look like this:
but looks like this after scrolling while editing:
You are initializing a UITableViewCell object first, then dequeuing from the table view? This is incorrect.
Try:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
I just tried this on a demo project and it behaves as expected.
I'm more familiar with this type of cell reuse:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
}
FieldItemDecrypted *theField = [decryptedArray objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [[NSString alloc] initWithData:theField.field encoding:NSUTF8StringEncoding];
cell.detailTextLabel.text = [[NSString alloc] initWithData:theField.type encoding:NSUTF8StringEncoding];
return cell;
}