Im currently working on my first "big" project, a currency converter. When i select a row i want every cell to change value of "nan" label to the currency returned from the xmlparse class. But, i don't know how i should call this piece of code from inside the didSelectRow method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"doingit");
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
XMLparse *parse = [[XMLparse alloc]init];
[parse setcurrencyname:[Datarray2 objectAtIndex:indexPath.row]];
cell.nameLabel.text = [Datarray2 objectAtIndex:indexPath.row];
cell.prepTimeLabel.text = [NSString stringWithFormat:#"%.9f",[parse convertionrate]];
cell.thumbnailImageView.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
return cell;
}
//didselectrow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath");
XMLparse *parse = [[XMLparse alloc]init];
[parse setHowmuchmoney:1];
[parse setValutanamn1:[Datarray objectAtIndex:indexPath.row]];
NSLog(#"%#",[parse valutanamn1]);
[parse loadDataFromXML];
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:[Datarray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[messageAlert show];
//call cellForRowAtIndexPath here?
}
You only have to do this:
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
Related
I want to use activity indicator to show loading activity, like this:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Create cell
static NSString *cellIdentifier = #"cell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCellView" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Check condition, either we got objects
if (!isDataLoaded){
cell.myActivityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.myActivityIndicator.center=self.view.center;
[cell.myActivityIndicator startAnimating];
[self.view addSubview:cell.myActivityIndicator];
NSLog(#"Current loading");
} else {
cell.myActivityIndicator.hidden = YES;
[cell.myActivityIndicator removeFromSuperview];
}
return cell;
}
However, its not vanish, even when i know that "else" block is executed. How to hide it?
I want to checkmark the data when the data is being loaded in the uitablecell. I know it works when you put it in the didselect section but can we use it in the cellForRowAtIndexPath. If yes how to use it? I have tried but it doesn't show any checkmarks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"AddGoalsTableViewCell";
AddGoalsTableViewCell *cell = (AddGoalsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AddGoalsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.addGoalsText.font = [UIFont fontWithName:#"AvenirNext-Medium" size:17];
cell.addGoalsText.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
cell.addGoalsText.text = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"name"];
PFObject *currentUser = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"user"];
User *user = [[User alloc] initWithUserId:[currentUser valueForKey:#"userID"] userName:[currentUser valueForKey:#"name"] anduserpassword:[currentUser valueForKey:#"password"]];
if(user==nil)
{
}
else if([[user userId]isEqualToString:[[NSString stringWithFormat:#"%#", [[[UIDevice currentDevice] identifierForVendor] UUIDString] ] lowercaseString]])
{
[self.tableView beginUpdates];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark; <------Here
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
return cell;
}
Please refer following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil )
{
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.lastIndexPath = indexPath;
[tableView reloadData];
}
And lastIndexPath is a property(retain) NSIndexPath* lastIndexPath;
I have a custom cell with a property that is an NSString called videoURl. My implementation code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
// Configure the cell...
cell.videoThumbnail.image = [UIImage imageNamed:[thumbNailArray objectAtIndex:indexPath.row]];
NSString *stringWithoutPng = [[thumbNailArray objectAtIndex:indexPath.row]
stringByReplacingOccurrencesOfString:#".png" withString:#""];
cell.titleLabel.text = stringWithoutPng;
cell.dateUploadLabel.text = [videoUploadDateArray objectAtIndex:indexPath.row];
cell.durationLabel.text = [videoLengthArray objectAtIndex:indexPath.row];
cell.viewsLabel.text = [videoViewsArray objectAtIndex:indexPath.row];
cell.videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(cell.videoURL);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RemedyYouTubeTableViewCell *cell = (RemedyYouTubeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *videoURL = cell.videoURL;
NSLog(videoURL);
NSLog(#"^videourl");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
When it logs the urls at the top everything is fine but when it is called at the bottom nothing prints. What am i doing wrong? Or is there a better way to do it? Thanks!
please replace your didSelectRowAtIndexPath method with this given code...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(videoURL);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
I am fetching data from a web service which returns an array and I am displaying that array in the table view. I am facing the issue that table values are getting updated but after some time as first it is showing the old value and upon scrolling it shows the new value.
Below is my code.
NSURLRequest *request=[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:8.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableDictionary *ResultdicNew = [[NSMutableDictionary alloc]initWithDictionary:(NSMutableDictionary *)responseObject];
NSString *message=[ResultdicNew objectForKey:#"status"];
NSString *message1=#"";
if([message isEqualToString:#"success"])
{
NSMutableDictionary *datadic=[ResultdicNew objectForKey:#"data"];
NSMutableDictionary *listDataDic=[datadic objectForKey:#"competitors"];
affArray=[listDataDic valueForKey:#"affiliation"];
countArray=[listDataDic valueForKey:#"total_count"];
_lblEventTitle.text=[datadic valueForKey:#"group_name"];
NSLog(#"affArray here is %#",affArray);
NSLog(#"countArray.count here is %ld",countArray.count);
[self revealCompetitorsView];
}
else
{
message1=[Resultdic objectForKey:#"data"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message1
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}
[SVProgressHUD dismiss];
// NSLog(#"JSON Here is :%lu",(unsigned long)Resultdic.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Data"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
#pragma mark - UITableView Datasource, Delegate Methods
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [countArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
}
[self.sideTable reloadData];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
}
Your problem is in your cellForRowAtIndexPath - you are not updating the cell when you are given an existing cell for reuse.
You need to move the updating of the cell fields outside of the if (cell==nil)... block -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}
Also, you can register your nib file for your cell in viewDidLoad and then you can use dequeueReusableCellWithIdentifer:forIndexPath which will automatically allocate a new cell if required and you can skip the whole if (cell==nil... bit.
You should also consider creating a UITableViewCell subclass and then you can assign your text fields to IBOutlet properties instead of searching by tag.
Replace this code on cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}
this is my first question, sorry if something is wrong
Well I'm trying to create a view in which i can select a friend from a table view and then it should say the number and the mail on a UIAlertView but I dont know how to do this
the friend list is obtained from an xml file on my site, and then is parsed an showed on a table with a custom cell design
this is the code that creates each cell
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"ContactListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactListItem" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:#"user"];
return cell;
}
thanks
Santiago
You have to implement the didSelectRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
NSString *name = [itemAtIndex objectForKey:#"user"];
NSString *email = [itemAtIndex objectForKey:#"email"];
NSString *phone = [itemAtIndex objectForKey:#"phone"];
NSString *messageStr = [NSString stringWithFormat:#"Email : %#\nPhone : %#", email, phone];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name message:messageStr delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath is the method you would need to implement. This is assuming the view you are presenting the table view in is the delegate for the table view.
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Alert view logic happens here getting all the cell information
}