PFQueryTableViewController with custom cell crashes on launch - ios

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.

Related

how can I adjust label height according to the text content in objective c

Below is the code that is not working:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
MessageObject *currentdatas = [self.objectHolderArray objectAtIndex:indexPath.row];
cell.Name_Label.text = currentdatas.Name;
//Here I wanted to adjust my label height according to my message received
cell.Message_Label.text = currentdatas.MSG;
[cell.Message_Label sizeToFit];
NSTimeInterval timeInterval = currentdatas.time/1000;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"My date %#",dateString);
cell.Time_Label.text = dateString;
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:currentdatas.Profile]]];
// Image Rectangle to circular
cell.Image_View.image = image;
cell.Image_View.layer.cornerRadius = cell.Image_View.frame.size.width/2;
cell.Image_View.clipsToBounds = YES;
return cell;
}
add this method.set label number of lines 0.
add this code in tableview
self.tableView.estimatedRowHeight = 80;
self.tableView.rowHeight = UITableViewAutomaticDimension;
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

How can I align 2 values Left and Right within the one cell?

I need some help with the below. I would like p.no to align to the left of the cell and p.name to align to the right. Any advice?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#", p.no , p.name];
return cell;
}
Follow These Steps:
1. drag & drop 2 labels on your prototypeCell in storyboard at the positions you want them to be in.
2. Create a subclass of UITableViewCell class in your project.
3. Assign newly created subClass as class for your table cell from identity inspector.
4. connect both label outlets with customTableviewcell.h
5. Import customTableViewCell.h in tableViewController.
6. Now in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method, replace code like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];
cell.label1.text = [NSString stringWithFormat:#"%# , p.no ];
cell.label2.text = [NSString stringWithFormat:#"%# , p.name ];
return cell;
}
Let me know if any issues are there :)

UITableView custom cell not showing core data

I had the table cells showing the data correctly, but then made a custom cell and class so that I would have more control over the layout...but I can't get my custom cell to show the data..it just shows the labels with their placeholder text...what am I missing?
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.results.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellResults";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
ResultsCell *resultscell = (ResultsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSManagedObject *result = [self.results objectAtIndex:indexPath.row];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *eventDate = [dateFormatter stringFromDate:[self trialDate]];
// formatting
resultscell.labelEventDesc.numberOfLines = 0;
// populate the cells
[resultscell.labelEventDesc setText:[NSString stringWithFormat:#"%#",[result valueForKey:#"eventdesc"]]];
[resultscell.labelWeeksOut setText:[NSString stringWithFormat:#"%#",[result valueForKey:#"weeksout"]]];
[resultscell.labelEventDate setText:[NSString stringWithFormat:#"%#",eventDate]];
return cell;
}
You're creating 2 different cell instances (cell and resultscell), configuring one and returning the other. So your custom class instance is never used for anything really.
Remove:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
and change return cell; to return resultscell;.

adding multiple contents in a cell

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

NSMutableArray and NSMutableDictionary in a TableView

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. :)

Resources