how to customize UITableViewCell - iphone

i want to customize table cell to show data like this (chk image)

For those rows which you want set color like above the simply set this color (shown in above pic) fo cell' background color.
or find this color use digital meter and use that rgb. like this
[UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1.0];
replace 200.0 with your rgb values
waht you need, you need to store data in common array for simply showing single detail you need to save that in form of string others in dictionary format.
And at time of showing data.
check wheter object is string or dictionary, and show accordingly.
for checking type of object use this
if([[myArray objectAtIndex:index] isKindOfClass:[NSString class]])

You can implement delegate method of viewForHeaderInSection: and implement view for yellow area shown in screenshot, and for for rest, you can implement custom table view cell inherited from UITableViewCell

You can subclass UITableViewCell.
In Xcode -> New File -> Objective-C class -> Subclass of UITableViewCell
Then in your tableview at cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
MySubclassedTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MySubclassedTableviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
return cell;

Use custom tableviewcell

Related

iPhone reusable cell does not show the changed data while scrolling

I have a table view. I have created a method that takes all the data it needs with a cell and returns the updated cell. In that method I have:
my cell for row goes like this
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
cell = myCellcreationMethod : cell : reuseIdentifier: other arguments;
MycellCreation method goes like this:
MycellCreation with args some strings , the cell and the reuseIdentifier.
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
//Here code that creates a custom image view something like:
thumbnailView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 9, 70, 70)];
thumbnailView = [imUtil getImageViewWithRoundedCorners:thumbnailView andRadius:10.0];
thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:thumbnailView];
//same here for another image view,
//same again
//and there same for a UILabel
}
//Here I set in each case the data for each imageView from above and the UIlabel text like
UIImage *thumbnailImage = [imUtil getThumbnailImageWithFile:thumbnail];
thumbnailView.image = thumbnailImage;
//the others go here
textLabel.text = #"something";
return cell;
All cells have the same identifier since they all are of the same type. But when I scroll down, instead of showing the data that corresponds to the next object in my list (new thumbnail new image, new text in uilabel) it replicates the first ones. I guess it loads the reused cell but does not put the new data in.
Any suggestions please?
I found the answer. I shouldn't just add the views with a method in my tableview class. I created a NEW subclass of UITableView, initialized it with my custom views and created a method in there that sets the new data. then used them two and it works like a charm!

Removing a checkmark accessory that is placed by default in my TableView

Brand new poster here on StackOverflow. I've been reading and learning here for some time, but I need to start asking a few questions and interacting, so any help with a few of my issues would be greatly appreciated.
I basically generate a list in a tableView of default sounds from a plist in cellForRowAtIndexPath. This generates me a list of say "x" sounds and one of those sounds has a default checkmark accessory placed on its cell. I have a plist file that stores the 'active' sound file's index value, this is how I put the original checkmark in the generated tableView. So, in the code below, as you can see, I load in the plist's value for the 'active' sounds index, however, when a user interacts with my tableView and they select a new sound, I need the default checkmark placed to be removed from view. Everything else is working fine. I can't seem to crack this simple problem, Im sure its a simple syntax issue, i just don't know how to get it done. Im sure the solution is a single line of code or two and its right under my nose. Thank you for help in advance.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionarySound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];
NSNumber *defaultSoundIndex = [dictionarySound valueForKey:#"soundIndex"];
int theIntVal = [defaultSoundIndex integerValue];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Everything below works just fine, checkmarks are removed and placed accordingly, sounds are played just fine. I just need help above in removing the default checkmark
if(self.checkedPath)
{
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:self.checkedPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedPath = indexPath;
....... other code below the plays my sounds on click and stores the newly active sound name and index value in the plist.
The usual way of dealing with this is to use didSelectRowAtIndexPath: to record the fact that the selection has changed and tell the table view to reload data, either all or just for the affected cells, then do the accessoryType configuration in cellAtRowForIndexPath:.
Other ways tend to fail if scrolling of the table is possible.
Alright boys and girls, here is the solution to my own question.
The checkmark is determined and placed in cellForRowAtIndexPath initially when the table is built based on my plist file. Here is the code for my cellForRowAtIndexPath that grabs my plist data, builds my table and sets the default selection with a checkmark.
NSDictionary *dicSound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];
NSNumber *defaultSoundIndex = [dicSound valueForKey:#"soundIndex"];
//conversion of NSNumber to integer for comparison below to indexPath.row, both need to be int's
int theIntVal = [defaultSoundIndex integerValue];
static NSString *CheckMarkCellIdentifier = #"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CheckMarkCellIdentifier];
cell.textLabel.text = [[MyArray objectAtIndex:indexPath.row] objectForKey:#"name"];
if(indexPath.row == theIntVal)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
When the user interacts with the tableView by clicking on another cell and didSelectRowAtIndexPath kicks in, this is how Im placing the new checkmark and how Im setting the old cell to have no accessory checkmark.
//This is a dictionary file that contains a number of values I need to grab
NSDictionary *dictionarySound = [NSDictionary dictionaryWithContentsOfFile:[self ActDataFilePath]];
NSNumber *defaultSoundIndex = [dictionarySound valueForKey:#"soundIndex"];
//conversion of NSNumber to integer for comparison below to indexPath.row, both need to be int's
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(self.checkedPath)
{
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:self.checkedPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedPath = indexPath;
//THE FIX
[tableView reloadData];
The key here is the [tableView reloadData]; line as it reloads the table with the new options checked and removes the original checkmark. Obviously, Im reloading data which means that I have persistence built in in a plist file. I basically update the local dictionary's value for 'soundIndex' EVERY TIME a user clicks on a cell and then write that to the persistent plist file.
Hope this helps anyone else who is having this issue.
Try following code
cell.accessoryType = UITableViewCellAccessoryNone;

UITableViewCell - view showing data on top of data when being reused

I'm using a custom UITableViewCell. They load up great, but when they get reused instead of replacing the text in the labels, they are some how writing over the top of them. Any ideas how to stop this behaviour?
I assume I'm not reseting the view correctly before it gets reused. I'm currently empty the labels so they have just a blank #"" string. But I still get the old text plus the new (very messy).
I'm certain there's an obvious solution to this (presently i just don't reuse the cell, but this isn't best practice and is slow on old devices), so if someone can help I'd be very grateful.
Thanks
ED
As requested here is the method for amending the cell
static NSString *CellIdentifier = #"Cell";
TransactionCellView *cell = (TransactionCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TransactionCellView" owner:self options:nil];
cell = tblCell;
}
[cell resetCell]; // clears the labels
[cell setData:[dataArray objectAtIndex:[indexPath row]]; // replaces the data and updates the labels
return cell;
}
Solved this issue!!! Amazingly simple when you know what the problem is/was.
I was using IB and "Clear Graphics Context" was not selected on my UILabels. So that is why the next text was just overlaying the old.
THanks guys for trying to help.
I suspect that you are adding the label as a subview programatically in your cellForRowAtIndexPath method. You need to make sure that you are not adding that subview every time the cell is recycled. Instead create the label only when creating a new cell (not when recycling a cell) then assign a tag value to the label and then in the future, when it gets recycled, retrieve the label by tag value and change its text.
You are correct in that the cells are being reused. Your code should be set up roughly using the following pattern:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* identifier = #"someidentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
// create the cell and add all subviews to it
} else {
// update the cell and access appropriate subviews to modify what is displayed
}
return cell;
}
The cell will be created the first time the identifier is used. For all subsequent requests, the cell is pulled from the UITableView cache (via dequeueReusableCellWithIdentifier), and you can then access its subviews either by tag, index, type, or whatever mechanism you choose.
Somewhat related is that you can have multiple cell identifiers, which allows you to create multiple instances of different cells depending on the data that you have. In one of my projects, I have 4 different cells, each dependent upon the number of lines of data that they will display (anywhere between 1 and 4). This helps ensure a smooth scrolling experience regardless of how many lines the cell has since the renderer doesn't have to worry about dynamically changing the height of the cell on the fly.
Try this..
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
for (UIView* subView in cell.contentView.subviews)
{
[subView removeFromSuperview];
}
// Your Code to customize cell goes here.
}

Problem in displaying properly in tableView cell by using label

In my application I parsed the data through NSXMLParser and made separate class to store that data from which i usually display the data. Everything works fine in simulator except the the title which is display in table cell with image. Images appears properly but the title not appear properly.
This is my code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableCell3" owner:self options:NULL];
cell = nibLoadedCell;
}
NewsInfo *aNewsInfo = [appDelegate.newsArray objectAtIndex:indexPath.row];
titleLabel.text = aNewsInfo.title;
imageLabel.image = aNewsInfo.smallImageData;
return cell;
}
and in this way i configured my TableCell3.xib
Now i want my title data in 2 rows in label of the TableCell3.
I already used both the way through xib attributes settings or through code.
Code which i used instead of this xib attributes settings are:-
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.numberOfLines = 2;
titleLabel.text = aNewsInfo.title;
Now Can any one help me to figure out this prob i want to display my title in two rows of the table view cell.
Thanks in Advance.
My Data is showing in this way...![enter image description here][2]
So i want to display this title fully it show's partially right now...
i want to display in two lines...
My guess is that you need to enlarge the label height.
Maybe the label height i not large enough to display 2 lines of text and that's could be the reason that you see only 1 line.
in interface builder try to enlarge the label height.
and maybe i am wrong ?
Good luck
Shani
Adjust frame of an UiLabel to have more space for two lines or decrease font size.

iphone code - custom table instead of default one

i'm using a default style table (UITableViewCellStyleSubtitle)
i want to add more then one detailTextLabel in each row,
how can i customize it?
code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
ARecord *aRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = aRecord.lDate;
cell.detailTextLabel.text = aRecord.WNum;
// Only load cached images; defer new downloads until scrolling ends
//(!aRecord.appIcon) - use icon
if (!aRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:aRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = aRecord.appIcon;
}
}
return cell;
}
The best way of doing this is to add a UILabel to the cell.contentView. You would do this when you initially create the cell. I've found two things to be especially helpful: to lay out the label on a table cell in a throwaway document in Interface Builder to determine the initial frame. It's also especially helpful to set the autoresizingMask so that the label will be resized appropriately when the cell is resized (due to autorotation, going into edit mode, etc.).
Finally, you'll need to set the table view's rowHeight to a higher value to accommodate the larger cells, otherwise you'll end up with overlapping cells.
Also, set a tag on your label to make it easy to retrieve with viewWithTag: when you go to update the text.
You could add the labels to cell.contentView.