I'm binding json datas. Json send me km (kilometer) format float . like ; km=850.564636
I want put in cell this km data. like 850.56
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"KmCellTableViewCell";
KmCellTableViewCell *cell = (KmCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSString *system = [[UIDevice currentDevice] model];
if ([system isEqualToString:#"iPhone Simulator"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"KmCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}else
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"KmCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
Vehicle *GetVehicle = (Vehicle *)[ResultVehicles objectAtIndex:indexPath.row];
cell.txtPlate.text = [GetVehicle Plate];
cell.txtKm.text = [NSString stringWithFormat:#"%# km",[GetVehicle km]]; return cell; }
i formatted another class. but not in cell.i have to formatted in cell. like this in -(void)FirstBinding{} method
Vehicle *GetVehicle = (Vehicle *)[ResultVehicles objectAtIndex:indexPath.row];
cell.txtPlate.text = [GetVehicle Plate];
cell.txtKm.text = [NSString stringWithFormat:#"%# km",[GetVehicle km]]; return cell;}double km = [[vehiclesList objectForKey:#"total_km"] doubleValue];
NSNumberFormatter* nf = [[NSNumberFormatter alloc] init];
nf.positiveFormat = #"#,##0.00";
NSString* s = [nf stringFromNumber: [NSNumber numberWithFloat: km]];
txtKilometer.text = [NSString stringWithFormat:#"%#",s];
Related
I'm using one type of Custom Table View Cell, but when other data is posted to my Table View, I want it to be displayed in a different Custom Table View Cell in the same table.
For example, I've created a chat in my Table View. However when certain details are posted, I want a separate cell design to display these details. See my code below so far.
My question: How can I write, "If field_swaptime in self.messages is empty, display ChatTableViewCell - if it contains data, display SwapDetailTableViewCell" ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ChatTableIdentifier = #"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = #"SwapDetailTableViewCell";
NSDictionary *data = [self.messages objectAtIndex:indexPath.row];
if ( [data objectForKey:#"field_swaptime"] == nil ) {
NSLog(#"THIS IS DATA %#", data);
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ChatTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *userName = [data objectForKey:#"name"];
[cell.sendingUser setText:userName];
NSString *messageBody = [data objectForKey:#"body"];
[cell.messageDisplayed setText:messageBody];
NSString *timeReceived = [data objectForKey:#"published at"];
NSLog(#"Message Received at %#", timeReceived);
[cell.timeStamp setText:timeReceived];
return cell;
}
else {
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SwapDetailTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *Time = [data objectForKey:#"field_swaptime"];
NSLog(#"This is time %#", Time);
[cell.startTime setText:Time];
NSString *TimeEnd = [data objectForKey:#"field_endswaptime"];
[cell.endTime setText:TimeEnd];
return cell;
}
}
You are basically looking to write your initial if as:
NSDictionary *data = [self.messages objectAtIndex:indexPath.row];
//
// if self.messages is nil, then data will be nil and we'll display
// a ChatTableViewCell as before. If data is valid, but there is no
// value associated with "field_swaptime", then again, we'll display
// a ChatTableViewCell. However, if self.messages IS valid and
// there IS a value associated with "field_swaptime" key, then the
// if clause fails, and we execute the else and return a
// SwapDetailTableViewCell.
//
if ( !data || ![data objectForKey:#"field_swaptime"] } {
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ChatTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *userName = [data objectForKey:#"name"];
[cell.sendingUser setText:userName];
NSString *messageBody = [data objectForKey:#"body"];
[cell.messageDisplayed setText:messageBody];
NSString *timeReceived = [data objectForKey:#"published at"];
NSLog(#"Message Received at %#", timeReceived);
[cell.timeStamp setText:timeReceived];
return cell;
}
else {
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SwapDetailTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *Time = [data objectForKey:#"field_swaptime"];
[cell.startTime setText:Time];
NSString *TimeEnd = [data objectForKey:#"field_endswaptime"];
[cell.endTime setText:TimeEnd];
return cell;
}
Bonus: if self.messages is valid, and data is valid, then you already have data and don't need to multiple calls to [self.messages objectAtIndex:indexPath.row]. (you should do a bounds sanity check on self.messages.count against indexPath.row before calling objectAtIndex:indexPath.row, but the defensive coding part is up to you to put together)
Objective C is pretty nifty in that calling a method on nil object, just returns nil. So, it is perfectly valid to ignore whether self.messages is valid or not, and just retrieve the data it contains. If it is valid (and your index is in bounds of the array), then a valid NSDictionary will be returned that you can use throughout the if/else clause.
The type of cell you get is determined by the type you register to correspond with the identifier. So, when you're setting up the table (say, in viewDidLoad)...
UINib *nib = [UINib nibWithNibName:#"ChatTableViewCell" bundle:nil]; // bundle:nil means main bundle
[self.tableView registerNib:nib forCellReuseIdentifier:#"ChatTableIdentifier"];
UINib *nib2 = [UINib nibWithNibName:#"OtherTableViewCell" bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier:#"OtherIdentifier"];
Then, in cellForRowAtIndexPath, you use the modern dequeue method, dequeueReusableCellWithIdentifier:forIndexPath:, which never returns nil and always returns an instance built from the nib you registered earlier...
// generically
NSString *identifier = (/*some condition*/)? #"ChatTableIdentifier" : #"OtherIdentifier";
// or alternatively, given the condition in your comment...
NSString *identifier = (!data[#"field_swaptime"])? #"ChatTableIdentifier" : #"OtherIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
// now cell is one of your two custom classes, built from their respective nibs.
Based on the same condition, you can cast cell to either kind, and configure it...
if (/*same condition as identifier*/) {
ChatTableViewCell *chatCell = (ChatTableViewCell *)cell;
// do your config for this type here
} else {
OtherTableViewCell *otherTypeCell = (OtherTableViewCell *)cell;
// do your config for this other type here
}
return cell;
Interesting predicament: Data for 'variant' is returned successfully, and for some reason it won't display in my cell's UILabel (the UILabel just appears empty, and yes, it's 'hooked up')? Is my currencyFormatter throwing this off somehow? Should I format this line differently? Help is appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PointsTableIdentifier = #"PointsCell";
PointsCell *cell = (PointsCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"PointsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
BUYProduct *productsbuy = self.products[indexPath.row];
cell.productLabel.text = productsbuy.title;
BUYProduct *productDescription = self.products[indexPath.row];
cell.productDescrip.text = productDescription.htmlDescription;
BUYProduct *pointValues = self.products[indexPath.row];
BUYProductVariant *variant = pointValues.variants.firstObject;
cell.priceLabel.text = [self.currencyFormatter stringFromNumber: variant.price];
NSLog(#"%#", variant.price);
}
return cell;
}
I have 3 custom table cells in my UITableView. The first two cells have set positions followed by a random number of cells.
For my row count I use :
return [stepLabel count] +2;
And for my tablecells I use :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *stepsCellIdentifier = #"StepsViewCell";
static NSString *descIdentfier = #"descViewCell";
static NSString *videoCellIdentfier = #"videoViewCell";
if( indexPath.row == 0 ) {
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:videoCellIdentfier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"videoViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.textLabel.text = [descLabel objectAtIndex:indexPath.row];
return cell;
}
else if( indexPath.row == 1 ) {
stepDescCell *cell = nil;
cell = (stepDescCell *)[tableView dequeueReusableCellWithIdentifier:descIdentfier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"descViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.descTextLabel.text = [descLabel objectAtIndex:indexPath.row - 1];
return cell;
}
else {
StepViewCell *cell = nil;
cell = (StepViewCell *)[tableView dequeueReusableCellWithIdentifier:stepsCellIdentifier];
if( !cell ) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StepsViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.stepTextLbl.text = [stepLabel objectAtIndex:(indexPath.row - 2)];
NSString *imageThumb = [thumbImg objectAtIndex:(indexPath.row - 2)];
[cell.thumbImage setImage:[UIImage imageNamed: imageThumb] forState:UIControlStateNormal];
return cell;
}
}
All this works perfectly, however when I try to send a string from the table across a segue to another UIViewController I get the following error:
index (-2 (or possibly larger)) beyond bounds (3)'
The prepare for segue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"imageBigShow"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
imageBigViewController *destViewController = segue.destinationViewController;
NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];
destViewController.imageBigString = largeImgString;
}
}
What am I doing wrong?
UPDATE
#implementation detailViewController
{
NSArray *thumbImg;
NSArray *stepLabel;
NSArray *descLabel;
NSArray *largeImg;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Find out the path of recipes.plist
NSDictionary *mapping = #{
#"How to wear a Kilt" : #"wearAKilt",
#"How to tie a cravat" : #"wearACravat",
#"How to wear a sporran" : #"wearASporran",
#"How to tie Ghillie Brogues" : #"wearTheShoes",
#"How to wear the accessories" : #"wearAccessories",
#"How to tie a cravat" : #"wearACravat",
#"How to Measure the Neck" : #"htmNeck",
#"How to Measure the chest" : #"htmChest",
#"How to Measure the waist" : #"htmWaist",
};
NSString *name = [mapping objectForKey:self.title];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
thumbImg = [dict objectForKey:#"thumbImg"];
stepLabel = [dict objectForKey:#"stepLabel"];
descLabel = [dict objectForKey:#"descLabel"];
largeImg = [dict objectForKey:#"largeImg"];
}
You have the following line:
NSString *largeImgString = [largeImg objectAtIndex:(indexPath.row - 2)];
If the user taps on the 1st or 2nd rows (index paths with row 0 or 1), this code will fail.
I have a UITableView that holds an FBProfilePictureView and a UILable,
my problem is when a user scrolls the picture rebuilds its self and it takes time to show the image it self, i want to know how can i create the FBProfilePictureView once and then when the user scrolls it won't build it self again.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Messages *msg = [messages objectAtIndex:indexPath.row];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *encodedDataObject = [def objectForKey:#"myUser"];
User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData: encodedDataObject];
if(![msg.wrote_id isEqualToString:user.fid])
{
NSString *idToImage = [self getOtherUserId];
NSString *CellIdentifier = #"Cell";
ChatWindowCell *cell = nil;
cell = (ChatWindowCell *)[tableChat dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ChatWindowCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ChatWindowCell class]])
{
cell = (ChatWindowCell *) currentObject;
cell.profile_image.profileID=nil;
if(cell.profile_image.profileID.length>0)
{
return cell;
}
break;
}
}
cell.profile_image.profileID = idToImage;
NSString *text = msg.msg;
cell.lblMessage.text=text;
return cell;
}
else
{
NSString *CellIdentifier = #"Cell";
ChatCellMe *cell = nil;
cell = (ChatCellMe *)[tableChat dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ChatCellMe" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ChatCellMe class]])
{
cell = (ChatCellMe *) currentObject;
NSLog(cell.prof_img.profileID);
cell.prof_img.profileID=nil;
if(cell.prof_img.profileID.length>0)
{
return cell;
}
break;
}
}
cell.prof_img.profileID = user.fid;
NSString *text = msg.msg;
cell.lblText.text=text;
isMessageFromMe=false;
return cell;
}
}
any help would be great guys...
thanks a lot.
I created an alternate view to solve this problem, see https://github.com/combinatorial/DBFBProfilePictureView
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;
}