- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell setBackgroundColor:[UIColor lightGrayColor]];
cell.textLabel.textColor = [UIColor lightTextColor];
cell.textLabel.backgroundColor = nil;
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.backgroundColor = nil;
// Get list of local notifications
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
// Display notification info
[cell.textLabel setText:notif.alertBody];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd 'at' HH:mm"];
NSString *dateString = [format stringFromDate:notif.fireDate];
NSString *textData = [[NSString alloc]initWithFormat:#"%#",dateString];
[cell.detailTextLabel setText:textData];
//[notif.fireDate description]
[self.tableView reloadData];
return cell;
}
Everything goes absoloutely normal if i DONT write "[self.tableView reloadData];", but if I do, seems like the cell is there but its not being displayed (the cell separator disappear according to the number of cells the tableview has)
I need to reloadData so the user won't need to get out of the view and load the view again to display updated information.
Any suggestion?
You dont need to add [self.tableView reloadData]; inside cellForRowAtIndexPath, this is absolutely wrong, since calling reloadData will again call cellForRowAtIndexPath which as you experienced will create very weird issues
In general you normally call [self.tableView reloadData]; whenever you want to change all or some of the contents of the UITableView
Create a function to check if new data is available (maybe implement a refresh button/pull to refresh). If new data is available call [self.tableView reloadData];.
Related
I have a PFQueryTableViewController subclass (DebateQueryViewController) that I want to use a custom cell with. The DebateQueryViewController is in a storyboard, and I have the .xib for my custom cell. I have also created .h and .m files for the cell, but I have not changed the code in them, except for adding the outlets for the labels, etc in the xib file.
In the viewDidLoad method of DebateQueryViewController.m file I have this:
[self.tableView registerNib:[UINib nibWithNibName:#"RipDebateTableCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"debateCell"];
I also have these two methods:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 73;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"debateCell";
RipDebateTableCell *cell = (RipDebateTableCell * )[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//[df setLocale:enUSPOSIXLocale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MMM dd, yyyy "];
NSString *my = [df stringFromDate:[object objectForKey:#"Date"]];
NSLog(#"%#", my);
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
cell.titleOf.text = [object objectForKey:#"Topic"];
cell.dateOf.text = [NSString stringWithFormat:#"Date: %#", my];
cell.typeOf.text = #"Debate";
cell.cellTop.image = [UIImage imageNamed:#"table-top"];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Everything was working perfectly until I tried implementing custom cells, but now the app crashes on the launch image every single time.
i am new in iphone devlpmnt.i just want to add current date in the cell. i need "notiftext" appear left of the cell and current date on the right.thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate *AppD = (AppDelegate *)[[UIApplication sharedApplication]delegate];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
}
MLNotifMessage *list = [AppD.storage objectAtIndex:indexPath.row];
cell.textLabel.text = list.notifText;
NSString *dteStr = [[NSString alloc]init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/London"]];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
dteStr = [dateFormat stringFromDate:list.date];
[dateFormat release];
cell.detailTextLabel.text = dteStr;
return cell;
}
Use any other style of the cell or use custom cell to fully customize the cell. I think for your question tabel view stye UITableViewCellStyleValue1 is enough.
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
cell.textLabel.text = list.notifText;
cell.detailTextLabel.text = [NSDate date];
use Custom TableViewCell. You can make custom tableViewCell by subclassing it
I have a problem with using an NSMutableArray and NSMutableDictionary.
I've created a quiz that when it stops, it should display the current score and date in a tableview. It should then display those values in a tableview. But nothing is displayed in the tableview when I load its viewController. The mutableArray is created in the viewDidLoad: method. Is my code correct, or am I missing something here?
- (IBAction)saveResult:(id)sender {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSLocalizedString(#"DATE", nil)];// #"HH:mm zz"];
dateNow = [NSDate date];
NSString *formattedDateString = [dateFormatter stringFromDate:dateNow];
// Add the quiz result to the array, reload the data in the tableview
highscoreAndDate = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%i points", score], #"kResult",
formattedDateString, #"kDate", nil];
[highscore addObject:highscoreAndDate];
[tableView reloadData];
}
My tableView methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [highscore count];
}
- (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];
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"HH:mm zz"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:#"kResult"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:#"kDate"];
return cell;
}
#Anoop Vaidya.
You got me going on the right track. I put the NSLog statement when the objects are added and it shows that the row count of my array is going up. So my code is correct.
I figured out that it was due to my connections in my XIB file (very embarrassing!). Should have spotted that my self. I had hooked up my tableview outlets to the wrong viewController and so the tableview was never populated.
Very sorry for have taken up your time and thanks to everybody who responded so quickly. Glad I got it fixed. :)
I created a custom UITableViewCell with a UITextField, UILabels and UIButtons. When I enter a value to a UITextField and scroll down, it disappears and shows in another random cell. The same problem happens with my UIButton, when I press it, the state is changing but after I scroll down, it disappears.
Here is my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"produktCell";
//NSString *CellIdentifier = [NSString stringWithFormat:#"produktCell %d",indexPath.row];
ProduktTableViewCell *cell =(ProduktTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"wird neu erstellt");
cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.produktnameLabel.text = [managedObject valueForKey:#"produktname"];
if ([[managedObject valueForKey:#"vondatum"] length] > 1){
cell.vonDatumLabel.text = [managedObject valueForKey:#"vondatum"];
cell.bisDatumLabel.text = [managedObject valueForKey:#"bisdatum"];
}else {
cell.vonDatumLabel.text = dateString;
cell.bisDatumLabel.text = dateString1;
}
cell.datumButton.tag = indexPath.row;
cell.warenkorbButton.tag = indexPath.row;
cell.menge.tag = indexPath.row + 437812;
cell.mengeLabel.text = [managedObject valueForKey:#"mengelabel"];
cell.produktnameLabel.tag = indexPath.row +22333;
cell.mengeLabel.tag =indexPath.row;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSLocale *german = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
[formatter setLocale:german];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];
NSNumber *firstNumber = [managedObject valueForKey:#"preis"];
cell.preisLabel.text = [formatter stringFromNumber:firstNumber];
return cell;
}
EDIT: i fixed my problem with the textfield. i just delete the method:
/*- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[self.tableView superview] endEditing:YES];
}*/
but there is still a problem with my button.
My best guess, is that your cells that are being re-used, are receiving the same behavior of the cell that you just interacted with. You can try something quick to see what I mean. Comment this:
Edit:
ProduktTableViewCell *cell =(ProduktTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"wird neu erstellt");
cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Put this instead:
ProduktTableViewCell *cell = [[ProduktTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
This way your new cells won't get the same "state" as the previous cell. What you need to do next, is to know which cell are you working on. Besides that you still need to reset the state of new cells. To finish, uncomment what I told you to comment (since you want your cells to be re-usable)
I'm programming a simple in-house economics app for our company, but I'm facing some problems. I populate a UITableView with information from dynamically generated objects like this:
- (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];
}
Payments *project = [appDelegate.projects objectAtIndex:indexPath.row];
if([project.parentProject isEqualToString:bottomTabBar.selectedItem.title]) {
NSLog(#"%# är lika med %# index: %d",project.parentProject, bottomTabBar.selectedItem.title, indexPath.row);
// Configure the cell...
NSMutableString *changeInValue = [[NSMutableString alloc] initWithFormat:#"%d",[[project.amountsek objectAtIndex:0] intValue]-[[project.amountsek objectAtIndex:1] intValue]];
if([changeInValue intValue] >= 0) {
[changeInValue insertString:#"+" atIndex:0];
cell.imageView.image = [UIImage imageNamed:#"up.png"];
} else {
cell.imageView.image = [UIImage imageNamed:#"down.png"];
}
NSMutableString *foreignCurrency = [[NSMutableString alloc] initWithString:#""];
if(![project.currency isEqualToString:#"SEK"]) {
[foreignCurrency appendFormat:#" - %#%d",project.currency,[[project.payments objectAtIndex:0] intValue]];
}
NSString *detailString = [[NSString alloc] initWithFormat:#"%#%d (%#)%#",#"SEK",[[project.amountsek objectAtIndex:0] intValue],changeInValue, foreignCurrency];
[changeInValue release];
[foreignCurrency release];
cell.textLabel.text = project.name;
cell.detailTextLabel.text = detailString;
[detailString release];
}
project = nil;
return cell;}
And everything works like a charm! However! When I press another tabButton I want it to reload the table and to display only the matched elements! (The matching works fine, the log prints out everything correctly) Although, the old table cells does not empty before the new ones are added.
Here's the code for the reload tabItem:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"Tab clicked: %d", item.tag);
[sourcesTable reloadData];
}
How do I solve this?
I'm new to programming for the iPhone and I could really use some help.
Please call [sourcesTable reloadData]; in viewWillAppear method of that ViewController.m
This call every time when your view is appears.