iOS: Section overlaps UITableView record - ios

I am trying to display three sections (#"Accounts and Tasks", #"Life Events", #"More") and relevant content in an UITableView. I am creating a custom UILabel for each row and try to display text.
Here is my code below. My issue is, The third section instead of displaying third section contents "Social Media", #"About Hop, #"Settingsā€, it is showing some first section contents and overlapping the text.
Could someone guide me to solve this please?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:#"My Accounts",#"Transfers/Deposit",#"Pay Bills", #"Investment Center",#"Claims Center",#"Locators",#"Loan Calculator", nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:#"Auto Circle",#"Home Circle",nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:#"Social Media",#"About Hop", #"Settings", nil];
self.keys = [NSArray arrayWithObjects:#"Accounts and Tasks", #"Life Events", #"More", nil];
NSArray *values = [NSArray arrayWithObjects:arrTemp1, arrTemp2, arrTemp3, nil];
self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.keys count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.keys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
NSLog(#"listData: %#", listData);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];
label.text = [listData objectAtIndex:[indexPath row]];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
[cell setUserInteractionEnabled:YES];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"SettingsMore"];
[cell.contentView addSubview:imv];
return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UILabel *label;
if(cell == nil) {
label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];
label.tag = 8888;
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
label = (UILabel *)[cell.contentView viewWithTag:8888];
label.text = [listData objectAtIndex:[indexPath row]];
}
If you set label.text in if(cell == nil), the text will not get updated on reusing the cell.

Related

ios tableview text edit data lost when scrolling

I am using UIview and added tableview.The api sending a data to show textfield in tableiveiw.I can show the textfield data in cell using coding bt the problem is when I scroll the tableview the text value is lost.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return subIncidentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellapi";
UITableViewCell *incidentCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSInteger row = [indexPath row];
NSDictionary *dic = [subIncidentArray objectAtIndex:row];
if (incidentCell == nil)
incidentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if ([[dic objectForKey:#"TemplateType"] isEqual:#1]) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 300, 30)];
textField.delegate = self;
textField.tag = 1;
textField.text = self.contentOfTextField;
[incidentCell addSubview:textField];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor grayColor];
if ([dic objectForKey:#"TextValue"] ==[NSNull null])
textField.text = [dic objectForKey:#""];
else
textField.text = [dic objectForKey:#"TextValue"];
}
return incidentCell;
}
I had the same error, first of all be shure that your subIncidentArray is not empty in cellForRowAtIndexPath.
Try to re-init data of this array in this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *incidentCell = [UITableViewCell alloc]init];
NSInteger row = [indexPath row];
NSDictionary *dic = [subIncidentArray objectAtIndex:row];
if ([[dic objectForKey:#"TemplateType"] isEqual:#1]) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 300, 30)];
textField.delegate = self;
textField.tag = 1;
textField.text = self.contentOfTextField;
[incidentCell addSubview:textField];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor grayColor];
if ([dic objectForKey:#"TextValue"] ==[NSNull null])
textField.text = [dic objectForKey:#""];
else
textField.text = [dic objectForKey:#"TextValue"];
}
return incidentCell;
}
Try this this may work.
Try like this, it may work
UITableViewCell *incidentCell = [[UITableViewCell alloc]init];

Tableview is not showing?

I am making tableview with different sections programmatically. I am using this code Please help me where i am wrong.
I have to show different sections with different cells ,sections of years and cells of movies.
in .h file
{
NSDictionary *movieTitles;
NSArray *years;
}
#property (nonatomic, retain) NSDictionary *movieTitles;
#property (nonatomic, retain) NSArray *years;
in .m file
#synthesize movieTitles;
#synthesize years;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]pathForResource:#"about" ofType:#"plist"];
NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
movieTitles = dic;
aboutTable = [[UITableView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height) style:UITableViewStyleGrouped];
aboutTable.delegate = self;
aboutTable.dataSource = self;
UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
[backButton addTarget:self action:#selector(backdb) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];
[self.view addSubview:aboutTable];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [years count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *details = [years objectAtIndex:section];
NSArray *titleSection = [movieTitles objectForKey:details];
return [titleSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier ];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *details = [years objectAtIndex:[indexPath section]];
NSArray *titleSection = [movieTitles objectForKey:details];
cell.textLabel.text = [titleSection objectAtIndex:[indexPath row]];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *details = [years objectAtIndex:section];
return details;
}
You are returning the [years count] for numberOfSectionsInTableView. Where you initialise the years?
And also in viewDidLoad, instead of movieTitles = dic, use the below code:-
movieTitles = [dic copy];
where is your "year" value. It count become Zero. and you have to alloc your value.so first of all add object to array.
e.g.years =[[NSArray alloc]initWithObjects:#"2001",#"2002",#"2003", nil];
Table view don't show beacuse "years is nil" when called
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [years count];
}
as the number of section return 0, so the tableveiw could not add.
resolve this issue.
add this line in viewDidLoad method
years = #[#"2015",#"2015"];
- (void)viewDidLoad {
NSArray *arrYear;
arrYear=[[NSArray alloc]initWithObjects:#"1991",
#"1992",
#"1993",
#"1994",nil]
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdetifier = [NSString stringWithFormat:#"Cell_%#",indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier ];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
cell.textLabel.text = [arrYear objectAtIndex:[indexPath row]];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return years.count;
}
arrYear=[[NSArray alloc]initWithObjects:#"1991",
#"abc",
#"xyz",
#"123",nil]
You have to initialise Your Array values in didload

UITableView not properly populating sections with NSArray contents

My Parse cloud code sends back JSON to my iOS app with the following structure:
What I want to do is iterate through this and create a new section in the UITableView for every object in this matchCenterArray.
In this instance, there are three objects in the array, each contains a Top 3 NSDictionary whose value is an array of 3 items, each of which is yet another array of properties. As you can see, I want it set up so that each section has 3 cells, one for each of the top 3 items of that respective matchCenterArray object. I then want it to pull the properties of each item and display it in each cell as the texLabel, detailTextLabel, and thumbnail.
I've tried using a for loop as a solution, but this displays the same item in all cells of the array. This is probably because I'm only looping through matchCenterArray objects, but not additionally looping through items in those objects, as can be seen here:
cell.textLabel.text = [[[[_matchCenterArray objectAtIndex:i] objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Title"];
What I was thinking of doing is nesting a for loop within this one, in place of objectAtIndex:0, but sending a message to a for loop doesn't work.
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
#interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView *matchCenter;
#end
#implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
_matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];
[PFCloud callFunctionInBackground:#"MatchCenterTest"
withParameters:#{
#"test": #"Hi",
}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[_matchCenter reloadData];
NSLog(#"Result: '%#'", result);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _matchCenterArray.count;
}
//the part where i setup sections and the deleting of said sections
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 21.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)];
headerView.backgroundColor = [UIColor lightGrayColor];
// _searchTerm = [[self.matchCenterArray firstObject] objectForKey:#"Search Term"];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)];
// headerLabel.text = [NSString stringWithFormat:#"%#", searchTerm];
// headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
// headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor lightGrayColor];
[headerView addSubview:headerLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = section + 1000;
button.frame = CGRectMake(300, 2, 17, 17);
[button setImage:[UIImage imageNamed:#"xbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
}
- (IBAction)deleteButtonPressed:(UIButton *)sender {
NSLog(#"Search Term: '%#'", _searchTerm);
[PFCloud callFunctionInBackground:#"deleteFromMatchCenter"
withParameters:#{
#"searchTerm": _searchTerm,
}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
NSLog(#"Result: '%#'", result);
}
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for (int i = 0; i<[_matchCenterArray count]; i++) {
// populate dictionary with results
//NSDictionary *matchCenterDictionary= [_matchCenterArray objectAtIndex:indexPath.row];
// title of the item
cell.textLabel.text = [[[[_matchCenterArray objectAtIndex:i] objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", [[[[_matchCenterArray objectAtIndex:i] objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[[_matchCenterArray objectAtIndex:i] objectForKey:#"Top 3"] objectAtIndex:0] objectForKey:#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
//imageView.frame = CGRectMake(45.0,10.0,10,10);
}
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
Forget about the loops, the delegate method already run on a loop where the iteration number is equal to the datasource count.
numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _matchCenterArray.count;
}
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return _matchCenterArray[section][#"Top 3"].count;
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row][#"Price"];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
return cell;
}
Don't harcode your array count, if your json changes, your code will need to change.
In the future you should look into loading the images asynchronously using a library like SDWebImage in order to avoid lags.
cellForRowAtIndexPath: is called once per numberOfRowsInSection:
You need to add numberOfSections as well:
it should look like this:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// the following will set the cell attributes for cells in each section
// to customize per section, use indexPath.section to get which section you are at
// title of the item
cell.textLabel.text = [[[[_matchCenterArray objectAtIndex:indexPath.row objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", [[[[_matchCenterArray objectAtIndex:indexPath.row] objectForKey:#"Top 3"] objectAtIndex:0]objectForKey:#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[[_matchCenterArray objectAtIndex:indexPath.row] objectForKey:#"Top 3"] objectAtIndex:0] objectForKey:#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
return cell;
}
Please refer to apple's tableView guide

Not inserting values probably in dynamically UITableView

i'm trying to create a dynamically fixtures which automatic updates using an API. The problem is it does connect the right sections with the right fixtures and the fixtures seem to duplicate in the different sections. Why is it not connecting the section date with the fixtures?
First i have an fixture array which contain all the fixtures.
[fixtures addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,#"date", time, #"time", competition_id,#"competition_id", home, #"home", away, #"away", nil]];
Then i have an array with contain all the dates with no duplicates ordered by date called reversedArray:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];
reversedArray = [[reverseOrder reverseObjectEnumerator] allObjects];
Then i have my view where i look
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, 22)];
[label setFont:[UIFont boldSystemFontOfSize:14]];
[label setTextColor:[UIColor whiteColor]];
NSString *dateString = [NSString stringWithFormat:#"%#",[reversedArray objectAtIndex:section]];
NSString *newString = [dateString substringToIndex:[dateString length]-15];
NSString *string =[NSString stringWithFormat:#"%#",newString];
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:13/255.0f green:78/255.0f blue:102/255.0f alpha:1.0f]];
return view;
}
The numberOfRowsInSection method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *dateString = [NSString stringWithFormat:#"%#",[reversedArray objectAtIndex:section]];
NSLog(#"%# = %#", [dateString substringToIndex:[dateString length]-15], [[fixtures objectAtIndex:section] valueForKey:#"date"]);
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"date == %#",[dateString substringToIndex:[dateString length]-15]]];
return filteredArray.count;
}
cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
}
take out this code from your if sentence
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
you are reusing cells so the condition cell=nil will no be true always.
Also use indexPath.section for to know which data you should take for fill your cell
Assign label values outside if (cell == nil) reuse cell Identifier not enter the loop after cell creation
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
return cell;
}
Change this method cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away"];
return cell;
}
If you are using custom cell then try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"FixtureCell";
FixtureCell *cell = (FixtureCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FixtureCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away"];
return cell;
}

Autoresizing UITableViewCell

I wan to know how can I resize my table view cell, and see all the message
My app is obtaining the messages from an xml on my server, and then it shows them, but if the message is longer than the text field it just display 3 dots
here is my code:
- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return ( messages == nil ) ? 0 : [messages count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 37;
}
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"ChatListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ChatListItem" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [itemAtIndex objectForKey:#"text"];
UILabel *stextLabel = (UILabel *)[cell viewWithTag:3];
stextLabel.text = [itemAtIndex objectForKey:#"text"];
UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:#"user"];
UILabel *suserLabel = (UILabel *)[cell viewWithTag:4];
suserLabel.text = [itemAtIndex objectForKey:#"user"];
if ([usernameText.text isEqualToString:userLabel.text]){
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"chat1#2x.png"]];
[cell setBackgroundView:img];
[img release];
textLabel.hidden=NO;
stextLabel.hidden=YES;
userLabel.hidden=NO;
suserLabel.hidden = YES;
}
else{
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"chat2#2x.png"]];
[cell setBackgroundView:img];
[img release];
textLabel.hidden=YES;
stextLabel.hidden=NO;
userLabel.hidden=YES;
suserLabel.hidden = NO;
}
return cell;
}
Thanks
You have to pass by heightForRowAtIndexPath like this logic (isn't a code ready):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
NSString *text = [itemAtIndex objectForKey:#"text"];
if(text is larger then what ever you want) {
return yourSpecificCellHeight;
}
return 37;
}
To calculate the size of text see this post on stackOverflow

Resources