iOS - Displaying an array of objects inside of a single cell - repeatedly - iphone

I have not found any good resource on here so I thought it would be great to get this up and out there... and stop me from pulling my hair out.
Acting like the app "Agenda" (calendar app) - I have a tableview with each cell representing a day. The next step is to place the eventsArray inside the appropriate cell - through cellForRowAtIndexPath.
I am only familiar with displaying the eventsArray with sections and rows - using each indexPath to represent an event... not an array of events. (Below I posted working code for others looking for help in that department, and as a reference).
It would be great if someone could put up some code to execute the eventsArray inside a single cell - I'm assuming it is a use of the cellForRowAtIndexPath I am not familiar with. Thank you in advance. Any questions just shoot.
{
#pragma mark - TableView stuff
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
A1Section *a1section = [self.sections objectAtIndex:section];
return [a1section.rows count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.sections count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
A1Section *a1section = [self.sections objectAtIndex:section];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE, MMM d, yyyy"];
NSString *myDateString = [formatter stringFromDate:a1section.date];
return [NSString stringWithFormat:#"%#", myDateString];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 47;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *timeLabel = nil;
UILabel *nameLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 13.0, 55.0, 15.0)];
[timeLabel setTag:1];
[timeLabel setBackgroundColor:[UIColor clearColor]];
timeLabel.textAlignment = UITextAlignmentLeft;
[timeLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:14]];
[timeLabel setTextColor:[UIColor blackColor]];
[timeLabel setLineBreakMode:NSLineBreakByClipping];
[timeLabel setNumberOfLines:1];
[cell.contentView addSubview:timeLabel];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85.0, 11.0, 200.0, 20.0)]; //x = 80.0, width = 235
[nameLabel setTag:2];
[nameLabel setBackgroundColor:[UIColor clearColor]];
nameLabel.textAlignment = UITextAlignmentLeft;
[nameLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:17]];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[nameLabel setNumberOfLines:1];
[cell.contentView addSubview:nameLabel];
} else {
timeLabel = (UILabel *)[cell.contentView viewWithTag:1];
nameLabel = (UILabel *)[cell.contentView viewWithTag:2];
}
A1Section *a1section = [self.sections objectAtIndex:indexPath.section];
PFObject *object = [a1section.rows objectAtIndex:indexPath.row];
NSDate *someDate = [object objectForKey:#"startDate"];
NSString *time = [self.dateFormatter stringFromDate:someDate];
[(UILabel *)[cell.contentView viewWithTag:1] setText:time];
[(UILabel *)[cell.contentView viewWithTag:2] setText:[object objectForKey:#"textContent"]];
return cell;
}
}

I'm not entirely sure what you're asking, but I presume you're asking how to use an array's contents for display in cellForRowAtIndexPath. If so, you would just have an array property that contains the data you want. Then, in cellForRowAtIndexPath, you can do something like
NSString *name = [self.dataArray objectAtIndex:indexPath.row];
nameLabel.text = name;
Does that answer your question?

Related

FPPopover cell.detailTextLabel.text

For those that are familiar with FPPopOver: https://github.com/50pixels/FPPopover
Can the cells display subtitle? I had to attach my UITableViewController to a nib.
In the Storyboard I have the cell set up to show subtitle, however, when the popOver appears on the iPhone, only cell.textLabel.text is visible - cell.detailTextLabel.text is not appearing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *entityName= [[managedObject entity]name];
cell.textLabel.text = [NSString stringWithFormat:#"%# %i", entityName, [indexPath row]];
cell.detailTextLabel.text = #"Subtitle";
}
Here is the image:
Update
After using Labels to display the subtitle, after scrolling the cell back into the view, the following happens:
if it's not working at all then alternative of that is you can use lable and set frame of lable as detail text has and add that lable as cell.contentview addsubview so this way you'll get your detail text on your tableview cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tblUser dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSMutableDictionary *objUser = [arrUser objectAtIndex:indexPath.row];
strName = [objUser objectForKey:#"Name"];
strDate = [objUser objectForKey:#"Date"];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(85, 10, 215, 20)];
lblName.text = strName;
lblName.textColor = [UIColor whiteColor];
[lblName setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblName];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(85, 34, 215, 20)];
lblDate.text = strDate;
lblDate.textColor = [UIColor whiteColor];
[lblDate setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblDate];
return cell;
}

Load more at the top of uitableview

I need a load more cell at the top of my table populated with custom cell, the first time that I populate the table is all ok, no overlap and no problem, if I try to load more cell this cell appear but with the wrong height and if I try to scroll the cell change position randomly!
This is my code, where the mistake for you?
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arrayMessaggi count] + 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelLoad = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[labelLoad setBackgroundColor:[UIColor clearColor]];
[labelLoad setTextAlignment:NSTextAlignmentCenter];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
[labelLoad setText:#"Load more..."];
[labelLoad setTag:3];
[cell.contentView addSubview:labelLoad];
}
UILabel *labelLoad = (UILabel *)[cell.contentView viewWithTag:3];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
float height = [self getStringHeight:[obj objectForKey:#"TestoMessaggio"] andFont:[UIFont systemFontOfSize:13]];
UILabel *labelUser = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
[labelUser setBackgroundColor:[UIColor clearColor]];
[labelUser setText:[user objectForKey:#"username"]];
[labelUser setFont:[UIFont systemFontOfSize:13]];
[labelUser setTag:1];
[cell.contentView addSubview:labelUser];
}
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
UILabel *labelUser = (UILabel *)[cell.contentView viewWithTag:1];
[labelUser setText:[NSString stringWithFormat:#"%d - %#",indexPath.row,[user objectForKey:#"username"]]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = 44;
if (indexPath.row > 0) {
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
NSString *text = [obj objectForKey:#"TestoMessaggio"];
height = 30 + [self getStringHeight:text andFont:[UIFont systemFontOfSize:13]] + 10;
}
return height;
}
- (void) setPullDownToRefresh {
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(aggiornaTabella:) forControlEvents:UIControlEventValueChanged];
[self.messagesTableView addSubview:refreshControl];
}
- (void) aggiornaTabella:(UIRefreshControl *)myRefreshControl {
self.arrayMessaggi = [NSMutableArray arrayWithArray:objects];
self.messagesTableView = [[UITableView alloc] init];
[self.messagesTableView reload data];
}

Indexpath.row not working while getting record from NSMutableArray

I'm trying to load the data UITableViewwhich is coming from webservice. Everything is working fine. But, whenver the record is being displayed in UITableView using -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delegate method i'm getting exception like below-
Here is my code -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell; //= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Reuse_Cell"];
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectedBackgroundView = [[UIView alloc]init];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
label = [[UILabel alloc]initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:1];
[label setFont:[UIFont fontWithName:#"Trebuchet MS" size:18]];
[label setTag:1];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
[[cell contentView] addSubview:label];
}
if (!label)
label = (UILabel *)[cell viewWithTag:1];
[label setText:[[webserviceRecords objectAtIndex:indexPath.row] objectForKey:#"catName"]];
[label setFrame:CGRectMake(10.0, 5.0, 225.0, 35.0)];
cell.selectedBackgroundView.backgroundColor = RGBACOLOR(151.0, 191.0, 69.0, 1.0);
cell.layer.borderWidth = 2.0f;
cell.layer.borderColor = RGBCOLOR(214, 218, 1).CGColor;
return cell;
}
My Array records -
http://pastie.org/7120406
Custom init method
- (id)initwithInteger:(NSInteger )integer
{
self = [super init];
if (self) {
startingpage = integer;
webserviceRecords = [[NSMutableArray alloc]init];
}
return self;
}
NumberofRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Row count - %d", [webserviceRecords count]);
return [webserviceRecords count];
}
You're setting the cell's selectedBackgroundView before checking that the cell has a value. You should check if the cell is nil first before doing that.
On a side note, you may find it much easier to display your array using the free Sensible TableView framework. The framework generates all the cells automatically from your array (including all the detail views), and can even fetch the data from the web service directly if you wish. Should save you a lot of headache and manual work. Hope this helps.
Really a silly mistake was there. I've parsed the JSON data directly into my NSMutableArray I've parsed like below first -
webserviceRecords = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];
Instead of this i've fixed this like below -
NSArray *array = nil;
array = [NSJSONSerialization JSONObjectWithData:mutableData options:0 error:&error];
webserviceRecords = [array mutableCopy];
It helps me to fix my issue. Thanks for responsing my question.
Cheers!

iphone:UItableviewcell duplicates when using table reload

My tableview gets duplicated when I use [tableview reloadData].I used this to refresh the tableview when I move back.How could I overcome the issue?
This is my code.
#define kMyTag 1
#define despTag 2
#define subTag 3
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
UILabel *mainLabel=[[UILabel alloc]init];
mainLabel.tag = kMyTag; // define kMyTag in your header file using #define
mainLabel.frame=CGRectMake(95, 0, 170, 30);
mainLabel.font = [UIFont boldSystemFontOfSize:14];
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];
UILabel *despLabel=[[UILabel alloc]init];
despLabel.tag=despTag;
despLabel.frame=CGRectMake(95, 25, 190, 20);
despLabel.font= [UIFont systemFontOfSize:12];
despLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:despLabel];
} else {
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
switch([indexPath section]){
case 0:
{
NSString *urlString = [img objectAtIndex:indexPath.row];
urlString=[urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
tname=[mname objectAtIndex:indexPath.row];
tprice=[mprice objectAtIndex:indexPath.row];
tquan=[mquan objectAtIndex:indexPath.row];
tspice=[mspice objectAtIndex:indexPath.row];
UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
mainLabel.text = [NSString stringWithFormat:#"%#",tname];
UILabel *despLabel=(UILabel *)[cell.contentView viewWithTag: despTag];
despLabel.text = [NSString stringWithFormat:#"Qty :%# Spice: %#",tquan,tspice];
UILabel *subLabel=(UILabel *)[cell.contentView viewWithTag: subTag];
float pric=[tprice floatValue];
subLabel.text = [NSString stringWithFormat:#"Price :%# %.2f",currencyType, pric];
}
}
}
Overcomes the issue by removing all the objects from array,then reselects the DB table & reloads uitableview.
it happens because you are not passing any value in dequeueReusableCellWithIdentifier
pass a string value and then use it in reuseIdentifier also

iphone sdk: Can't reuse cell with viewWithTag (can not setText to reused labels)

This is my cellForRowAtIndexPath function. I could not get the setText to the label to work. Can you please help me out?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *messageLabel = nil;
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, ROWHEIGHT) reuseIdentifier:CellIdentifier];
messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 240, 60)];
[messageLabel setFont:[UIFont fontWithName:#"ArialMT" size:12]];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setNumberOfLines:3];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTag: messageTag];
[cell.contentView addSubview:messageLabel];
}
else{
messageLabel = (UILabel *)[cell viewWithTag:messageTag];
}
[messageLabel setText:[[[aSingleton wallItemArray] objectAtIndex:row] message]];
NSLog(#" -- > at row %d, message: %#", row, [[[aSingleton wallItemArray] objectAtIndex:row] message]);
return cell;
}
You're adding the UILabel to the cell's contentView, but asking the cell for the view.
Have you tried:
messageLabel = (UILabel *)[cell.contentView viewWithTag:messageTag];
EDIT: Also, you have two memory leaks - you're alloc'ing a UITableViewCell and a UILabel without (auto)releasing them anywhere.