Issue with custom TableViewCells in table display from NSMutableArray - ios

I'm having a weird issue - in my code I am fetching data from the internet in XML format and fill a NSMutableArray (altogether 34 items).
This data should be displayed in a TableView. I created a custom TableViewCell for that. However it always only displays the first 10 items (0-9), then the 11th item will change as you scroll up the table (starting from 11 and changing itself to 12, 13, 14 ...), then it will display the first 10 items again.
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [myCurrencies count];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil];
cell = tableViewCell;
cell.accessoryType = UITableViewCellAccessoryNone;
self.tableViewCell = nil;
}
NSLog(#"%i",indexPath.row);
actualRate.text = [NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]];
currencyCode.text = [NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]];
countryFlag.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]];
return cell;
}
However when I just set the textlabel of the standard cell everything is fine and I am displayed 34 table rows with numbers 0 - 33:
cell.textLabel.text = [NSString stringWithFormat:#"%i",indexPath.row];
Any idea out there?

your cell initialization is wrong
first of all name all your labels and image views in Interface Builder with tag
for example actualRate - tag 101
currencyCode tag 102
countryFlag tag 103
then
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil] lastObject];
}
NSLog(#"%i",indexPath.row);
[((UILabel *)[cell viewWithTag:101]) setText:[NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]]];
[((UILabel *)[cell viewWithTag:102]) setText:[NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]]];
[((UIImageView *)[cell viewWithTag:103]) setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]]];
return cell;
}

Related

Add Custom Cell Randomly in UITableView

I have a UITableView which is populated with an NSArray. The array is sorted alphabetically in my table with alphabetized headers.
I am adding a custom cell randomly in my table at every 10th row.
The issue I have is that the custom cell is layering over actually data, so it's not being inserting within the array.
How can I solve this?
Here's what I am doing to insert the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 10 == 10-1) {
CustomAdCell *cell = (CustomAdCell *)[tableView dequeueReusableCellWithIdentifier:#"AdCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomAdCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
else {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *key = [[[self indexedMembers] allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)][indexPath.section];
self.indexedMembersArray = ((NSMutableArray *)[self indexedMembers][key]);
MemberInfo *currentMember = self.indexedMembersArray[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currentMember.firstName, currentMember.lastName];
return cell;
}

Modifying one cell in CellForRowAtIndexPath changes multiple cells

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
}

First 3 cells are the same but turn correct once I start scrolling?

So Whenever I launch my app it looks like this and the first 3 cells are all the same. But once I start scrolling up and down it fixes and cell 2 and 3 show the correct information.
This is currently how my code looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyFeedCell";
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SampleTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SampleTableCell *)currentObject;
break;
}
}
}
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here's a gif of how it's currently functioning
http://gyazo.com/2011099496257445c008a717beabd8fd
The whole topLevelObjects gambit is no longer necessary, replace your code with this and see if your still getting the problem:
//this above #interface
static NSString *CellIdentifier = #"MyFeedCell";
//Put this in viewDidLoad
[self.table registerClass:[SampleTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.table registerNib:[UINib nibWithNibName:#"SampleTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Use a custom UITableViewCell for many UITableView

I have created a custom UITableViewCell for a UITableView by dragging and dropping the labels and imageView from my Prototype Cell to a Custom Cell which I call QuizInfoCell.
That works fine for the particular TableView that I created the Prototype Cell in, but I can't get QuizInfoCell to work for any other TableView. How can I achieve that?
This is the code that I'm using in the TableView that created the Prototype Cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
cell = [[QuizInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"QuizInfoCell"];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}
I suggest creating your QuizInfoCell as a new XIB file.
Press CMD + N
Select the User Interface submenu.
Create a new View and call it QuizInfoCell
In your new XIB delete the existing View element and instead drag and drop a new Table View Cell.
Recreate your custom UITableViewCell in the same way as you did before by dragging and dropping the labels and imageView
Finally give the new Cell an Identifier called QuizInfoCell, like this:
You can then load your new Custom UITableViewCell with Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuizInfoCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"QuizInfoCell" forIndexPath:indexPath];
if (cell == nil){
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"QuizInfoCell" owner:self options:nil];
cell = [nibContents objectAtIndex:0];
}
cell.name.text = [title objectAtIndex:indexPath.row];
cell.author.text = [author objectAtIndex:indexPath.row];
cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
cell.plays.text = [#"Plays: " stringByAppendingString:[NSString stringWithFormat:#"%#", [plays objectAtIndex:indexPath.row ]]];
cell.ratingUps.text = [NSString stringWithFormat:#"%#", [ratingUps objectAtIndex:indexPath.row]];
cell.ratingDowns.text = [NSString stringWithFormat:#"%#", [ratingDowns objectAtIndex:indexPath.row]];
cell.qid.text = [NSString stringWithFormat:#"%#", [qid objectAtIndex:indexPath.row]];
return cell;
}

Changing between UITableViewCells in UITableView

I am new to UITableViews, and I dont know very much about this topic.
I have two buttons in the same View, but not in the Table, for control the switch between the UITableViewCells, when one button is pressed then set a boolean for load the specific Cell.
If you click the button 1 then it shows the table with the CellType1.
If you click the button 2 then it shows the table with the CellType2.
My goal is use the same Table for this. But I dont know where to start.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
if (clickButton1 == true)
{
clickButton1 *cell = (clickButton1*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = #"Trial";
return cell;
}
else if (clickButton2 == true)
{
clickButton2 *cell = (clickButton2*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.name.text = #"Trial";
return cell;
} else
I think that this code is not the best. But I am lost at this point.
Thanks in advance!
using a tutorial example like this...
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
if you have two arrays for items such as self.tweetArray1 and self.tweetArray2 you could change this code to
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int i = buttonNumberClicked;
return [self.tweetsArray[i] count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
//5
int i = buttonNumberClicked;
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray[i] objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
and set up ibactions for your buttons to change the variable/int buttonNumberClicked to 1 or 2 depending on which was pressed and [tableView reloadData]; inside ibaction as well, hope this helps.

Resources