- (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];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
Change your cell's style to UITableViewCellStyleDefault
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
If you want two labels in the cell, then add it to the contentView of the 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];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
UIlabel *detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(60,10,200,50)];
[detailLbl setText:[[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];];
[cell.contentView addSubView:detailLbl];
cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
return cell;
}
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];
}
// Configure the cell...
NSDictionary *temp = [self.placeArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [temp objectForKey:#"name"];
cell.detailTextLabel.text = [[temp objectForKey:#"distance"] stringByAppendingString:#" km from Banglore"];
//cell.imageView.image =[UIImage imageNamed:[temp objectForKey:#"category"]];
//write this code
// Load the image with an GCD block executed in another thread
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
UIImage * image = [UIImage imageNamed:[temp objectForKey:#"category"]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell setNeedsLayout];
});
});
dispatch_release(downloadQueue);
return cell;
}
Related
I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}
I want to checkmark the data when the data is being loaded in the uitablecell. I know it works when you put it in the didselect section but can we use it in the cellForRowAtIndexPath. If yes how to use it? I have tried but it doesn't show any checkmarks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"AddGoalsTableViewCell";
AddGoalsTableViewCell *cell = (AddGoalsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AddGoalsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.addGoalsText.font = [UIFont fontWithName:#"AvenirNext-Medium" size:17];
cell.addGoalsText.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
cell.addGoalsText.text = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"name"];
PFObject *currentUser = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"user"];
User *user = [[User alloc] initWithUserId:[currentUser valueForKey:#"userID"] userName:[currentUser valueForKey:#"name"] anduserpassword:[currentUser valueForKey:#"password"]];
if(user==nil)
{
}
else if([[user userId]isEqualToString:[[NSString stringWithFormat:#"%#", [[[UIDevice currentDevice] identifierForVendor] UUIDString] ] lowercaseString]])
{
[self.tableView beginUpdates];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark; <------Here
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
return cell;
}
Please refer 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.lastIndexPath = indexPath;
[tableView reloadData];
}
And lastIndexPath is a property(retain) NSIndexPath* lastIndexPath;
I am using two UITableView in same View. I have used prototype cell in which there are two label to show date and venue in TableView.
Here is the code what I did. Both TableViews showing same data.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableViewOne) {
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableViewtwo){
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Your if condition is incorrect. So please check like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView.tag == 5) // Change here
{
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else
{
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Do it like
if (tableView == tableViewOne){
// Your Code
}
elseif (tableView == tableViewTwo){
// Your Code
}
Your comaprison is incorrect since both table view are present, first condition get satisfied always thus same data in both tables.
Compare using UITableView object identifiers
if (tableView==first_table_view_name) {
}
else
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
//UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableView.tag==5) // problem is here
{
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableView.tag==6){
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (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.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
return cell;
}
So far search on Stack Overflow I havent found a situation that is like mine. Any assistance is greatly appreciated: I keep seeing that if I put a checkmark on Person A, Person H will also have one as well as does a person about 10 away. Basically abut every 10 it repeats a check mark.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text =
[NSString stringWithFormat:#"%# %#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"FirstName"],[[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"LastName"]];
cell.detailTextLabel.text =
[NSString stringWithFormat:#"%#", [[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"Address"]];
return cell;
}
In my did select row for index path I have this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [self.tableView cellForRowAtIndexPath: indexPath];
if ([[myArrayOfAddressBooks objectAtIndex:indexPath.row] objectForKey:#"emailSelected"] != #"YES")
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"YES" forKey:#"emailSelected"];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[[myArrayOfAddressBooks objectAtIndex:indexPath.row] setValue:#"NO" forKey:#"emailSelected"];
}
This is due to how UITableView "recycles" UITableViewCell for efficiency purposes, and how you are marking your cells when they are selected.
You need to refresh/set the accessoryType value for every cell you process/create within tableView:cellForRowAtIndexPath:. You properly update the state in your myArrayOfAddressBooks data structure, and you just need to use this information in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *info = [myArrayOfAddressBooks objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", [info objectForKey:#"FirstName"],[info objectForKey:#"LastName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [info objectForKey:#"Address"]];
cell.accessoryType = ([[info objectForKey:#"emailSelected"] isEqualString:#"YES"]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
Also, unless there is a good reason for saving the state as #"Yes" or #"No" strings, why not save them as [NSNumber numberWithBool:YES] or [NSNumber numberWithBool:NO]? This will simplify your logic when you want to do comparisons versus having to use isEqualToString: all the time.
e.g.
cell.accessoryType = ([[info objectForKey:#"emailSelected"] boolValue]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;