I have two UITableViewControllers, A and B. When I tap one cell in table A, I will use UINavigationController to push table view controller B. But the data of B is downloaded from Internet, which takes several seconds. So I want to add a UIActivityIndicatorView when loading B. How can I achieve this?
You can add UIActivityIndicatorView as cell's accessoryView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
}
In viewDidLoad of tableview B class, add an activity indicator.
// Create the Activity Indicator.
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
// Position it at the center of the ViewController.
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)])
activityIndicator.startAnimating()
Now call your method that downloads data from the network.
myDownloadMethod()
Do it in a different thread if you don't want the UI to be non responsive during the process.
read this thread for that.
Can I use a background thread to parse data?
When you are notified that the contents are downloaded, stop the indicator.
activityIndicator.stopAnimating()
Now you can call tableview.reloadData() for reloading the table to display the new contents.
UIActivityIndicatorView * activityindicator1 = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 200, 30, 30)];
[activityindicator1 setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityindicator1 setColor:[UIColor orangeColor]];
[self.view addSubview:activityindicator1];
[activityindicator1 startAnimating];
[self performSelector:#selector(callfunction) withObject:activityindicator1 afterDelay:1.0];
-(void)callfunction
{
// Here your stuf
}
It's work well for me, you can try it:
Call [activityIndicator startAnimating] when didHighlightRowAtIndexPath,
and call [activityIndicator stopAnimating] when didUnhighlightRowAtIndexPath useful than didSelectRowAtIndexPath.
- (void)runIndicatorAtIndexPath:(NSIndexPath *)indexPath display:(BOOL)playing{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = activityIndicator;
playing == YES ?[activityIndicator startAnimating]:[activityIndicator stopAnimating];
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
[self runIndicatorAtIndexPath:indexPath display:YES];
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
[self runIndicatorAtIndexPath:indexPath display:NO];
}
This code below will display a spinner at the footer of the table view if more data is available on the server. You can change it according to your logic of fetching data from server.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* set cell attributes here */
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
if(isMoreDataAvailableOnserver)
{
[self showSpinnerAtFooter];
[self getMoreDataFromServer];
}
else {
[self hideSpinnerAtFooter];
}
}
return cell;
}
- (void)hideSpinnerAtFooter {
self.tableView.tableFooterView = nil;
}
- (void)showSpinnerAtFooter {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.tableView.tableFooterView = spinner;
}
Related
I have a UITableView that has cells with images, text and a disclosure button. I'm loading the cells like this:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if ([indexPath row] == [[self multas] count]) {
[[cell textLabel] setText:#"Carregar mais..."];
}
else
{
Multa *multa = [self.multas objectAtIndex:indexPath.row];
NSString *dataIsolada = [[NSString stringWithFormat:[multa dataOcorrencia]] substringWithRange:NSMakeRange(0, 10)];
[[cell textLabel] setText:[NSString stringWithFormat:dataIsolada]];
[[cell detailTextLabel] setText:[multa descricao]];
[cell.imageView setImageWithURL:[NSURL URLWithString:[multa fotoURL]]
placeholderImage:[UIImage imageNamed:#"carregando.jpeg"]];
[cell.imageView setContentMode: UIViewContentModeScaleAspectFit];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}
When the user taps the disclosure button, I switch the disclosure button for an Activity Indicator and then push another view to the screen using a navigation controller:
- (void)carregarDetalhesMulta:(UITableView *)tableView comMulta:(Multa *)multa
{
DetalheMultaViewController *form = [[DetalheMultaViewController alloc] initWithMulta:multa];
form.delegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:form];
[self presentModalViewController:navigation animated:YES];
}
- (void) tableView: (UITableView *) tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *) indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
cell.accessoryView = spinner;
[spinner startAnimating];
Multa *multa = [self.multas objectAtIndex:indexPath.row];
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[spinner stopAnimating];
[self carregarDetalhesMulta:tableView comMulta:multa];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
});
}
Note that I've changed back the accessory for a disclosure button in the end. The problem is: when I come back(by tapping the button back) to the view that contains the UITableView, the line that I've tapped show no disclosure button nor Activity Indicator. What I am doing wrong? What's the better way to switch these controls without having to reload the table view?
The accessoryView property has priority over the accessoryType property. If you set the accessoryView to nil, you'll see the accessory button again.
Here's what I understood from the question: You'd like the accessory view to be a disclosure button that when tapped, becomes an activity indicator. After a short delay, you'd like to push to a new view controller.
Here's how I would do it:
setup the prototype cell in storyboard to have Accessory:none.
add a push segue from your view controller to the destination vc you want, give it an identifier, say #"MyPushSegue"
Add the accessoryView to cells (if they don't have one) in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
// the rest of your cellForRowAtIndexPath
if (!cell.accessoryView)
cell.accessoryView = [self createAccessoryView];
return cell;
}
Create the accessory view this way (assuming ARC):
- (UIView *)createAccessoryView {
UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectZero];
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
disclosureButton.tag = 100;
[disclosureButton addTarget:self action:#selector(pressedAccessory:) forControlEvents:UIControlEventTouchUpInside];
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
aiv.tag = 101;
aiv.alpha = 0.0;
aiv.center = disclosureButton.center;
accessoryView.bounds = disclosureButton.bounds;
[accessoryView addSubview:aiv];
[accessoryView addSubview:disclosureButton];
return accessoryView;
}
The disclosure button in the above triggers a method when the button is pressed. This method should change the state of the selector view and trigger the push segue you setup in storyboard:
- (void)pressedAccessory:(UIButton *)sender {
UIView *accessoryView = sender.superview;
[self setAccessoryView:accessoryView waiting:YES];
// let the user see the spinner for 1 seconds
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[self performSegueWithIdentifier:#"MyPushSegue" sender:self];
[self setAccessoryView:accessoryView waiting:NO];
});
}
Finally, to solve the problem you were having, we don't replace the accessory view with a spinner, we change how it looks by hiding/showing the button and hiding/showing the spinner, depending on what state we want:
- (void)setAccessoryView:(UIView *)accessoryView waiting:(BOOL)waiting {
UIButton *disclosureButton = (UIButton *)[accessoryView viewWithTag:100];
UIActivityIndicatorView *aiv = (UIActivityIndicatorView *)[accessoryView viewWithTag:101];
[UIView animateWithDuration:0.1 animations:^{
disclosureButton.alpha = (waiting)? 0.0 : 1.0;
aiv.alpha = (waiting)? 1.0 : 0.0;
}];
if (waiting) [aiv startAnimating]; else [aiv stopAnimating];
}
hi i have problem when i am displaying the records of table by clicking on load more cell . problem is this when i click on load more cell then table must load two more records and then load more text must move forward for example first time 2 records load on table at the 3rd cell is the load more cell when i click on this cell then two more records must load into table and this load more text move to 5th cell . but in my case the problem is when i click on load more cell at 3rd position it also move to fifth position but on 3rd cell record not load and also load more text appear on 3rd cell. kindly tell me how i can handle this problem this is the code
#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "song.h"
#import "BookDetailViewController.h"
#implementation RootViewController
#synthesize mylabel,mylabel1,mylabel2,spinner,cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myarray count]>pageSize)
{
return pageSize+1;
}
return myarray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
UIImage *image=[UIImage imageWithData:data];
if(indexPath.row<pageSize)
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.image=image;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.text = asong.title;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.text = asong.artist;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.text = asong.duration;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
else
{
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if (indexPath.row==pageSize)
{
pageSize=pageSize+2;
[cell reloadInputViews];
[tableView reloadData];
}
/* else if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:[NSBundle mainBundle]];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
bdvController.aartist = asong;
[self.navigationController pushViewController:bdvController animated:YES];*/
}
- (void)viewDidLoad {
[super viewDidLoad];
pageSize =2;
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
myarray = [[NSMutableArray alloc] initWithObjects:#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png", nil];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = #"Songs";
}
/*
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[bdvController release];
[appDelegate release];
[super dealloc];
}
#end
Your problem is that you're only configuring a cell if it's nil. In your example, you will want to replace cell 3's content with some song even when it's not nil.
When deque returns nil, create a new cell and add subviews but always update the content based on the path.
Try below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row<pageSize) {
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.tag = 1;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.tag = 2;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.tag = 2;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.tag = 2;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
UIImageView *imageView = [cell viewWithTag:1];
UILabel *mylabel1 = [cell viewWithTag:1];
UILabel *mylabel2 = [cell viewWithTag:1];
UILabel *mylabel = [cell viewWithTag:1];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:data];
});
});
mylabel1.text = asong.title;
mylabel2.text = asong.artist;
mylabel.text = asong.duration;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
static NSString *CellIdentifier = #"cellloadmore";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I need to load some data in a table view and while this is going on in the background I want to add an activity indicator in order to show that there is a process going on and will hide once the process finishes. What would be the most efficient way to implement something like this?
Depends, whether you want to block your user or not and also how important is the activity indication.
If you don't want to block user, use Application.networkActivityIndicatorVisible, if you want to have larger activity indicator and still not to block user, animate UIView with text and UIActivityIndicator below the table view (tableview.height -= activityview.height) and then hide on complete or if you would like to block user, use blocking activity indicator.
http://www.dejal.com/developer/?q=developer/dsactivityview
https://github.com/jdg/MBProgressHUD (I was using MBProgressHUD personally and it's easy to learn and use)
You can add a view which has a UIIndicatorView and a UILabel as your cell's subview. You can use this way to show error data loading/ error network/ empty data...
Example:
Your Controller can define two modes: UITableViewModeMessage and UITableViewModeData.
In viewDidLoad, you set self.tableViewMode = UITableViewModeMessage. When has returned data, set self.tableViewMode = UITableViewModeData and reload data for tableview.
Some code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableViewMode == UITableViewModeMessage) {
return 2;
} else {
return self.yourEntries ? self.yourEntries.count : 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.tableViewMode == UITableViewModeMessage) {
return [self tableView:tableView messageCellForRowAtIndexPath:indexPath];
} else {
return [self tableView:tableView dataCellForRowAtIndexPath:indexPath];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove Loading... progress view if exist.
UIView *progressView = [cell viewWithTag:100];
[progressView removeFromSuperview];
if (self.tableViewMode == UITableViewModeMessage) {
if (indexPath.row == 1) {
// remove the current label.
cell.textLabel.text = nil;
// We build progress view and attach to cell here but not in cellForRowAtIndexPath is because in this method cell frame is already calculated.
UIView *progressView = [self progressViewForCell:cell message:#"Loading..." alpha:0.9];
[cell addSubview:progressView];
}
}
}
// cell to display when loading
- (UITableViewCell *)tableView:(UITableView *)tableView messageCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 1) {
cell.textLabel.text = #"Loading...";
} else {
cell.textLabel.text = nil;
}
return cell;
}
// cell to display when has data
- (UITableViewCell *)tableView:(UITableView *)tableView dataCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DataCell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.yourEntries objectAtIndex:indexPath.row] description];
return cell;
}
// Build a view which has a UIActivityIndicatorView and a UILabel
- (UIView *)progressViewForCell:(UITableViewCell *)cell message:(NSString *)message alpha:(CGFloat)alpha
{
// NOTE: progressView needs to be removed from cell in cellForRowAtIndexPath:
CGRect progressViewFrame = CGRectZero;
progressViewFrame.size.width = CGRectGetMaxX(cell.bounds);
progressViewFrame.size.height = CGRectGetMaxY(cell.bounds) - 2;
UIView *progressView = [[UIView alloc] initWithFrame:progressViewFrame];
progressView.backgroundColor = RGBA(255, 255, 255, 1);
progressView.alpha = alpha;
progressView.tag = 100;
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:progressView.bounds];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.font = [UIFont systemFontOfSize:14];
loadingLabel.textColor = [UIColor blackColor];
loadingLabel.textAlignment = UITextAlignmentCenter;
loadingLabel.text = message;
CGFloat widthOfText = [loadingLabel.text sizeWithFont:loadingLabel.font].width;
CGFloat spaceBetweenIndicatorAndLabel = 5;
// activityIndicatorView has size in which width and height is equal to 20.
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicatorView setCenter:CGPointMake(CGRectGetMidX(cell.bounds) - (widthOfText / 2) - (activityIndicatorView.bounds.size.width / 2) - spaceBetweenIndicatorAndLabel, CGRectGetMidY(cell.bounds))];
[activityIndicatorView setColor:[UIColor blackColor]];
[activityIndicatorView startAnimating];
[progressView addSubview:activityIndicatorView];
[progressView addSubview:loadingLabel];
return progressView;
}
I have a table view that has many cells. Each cell has its own UITextField. I added the text fields programmatically. I want each and every textField to appear when the edit button is hit. (now table is in edit mode) and when pressed again, i want all of the textFields to vanish(leaving the edit mode). I know that i can accomplish this using the hidden property, but i tried doing this in this method:
- (IBAction)editButton:(id)sender
{
if (self.editing)
{
[self setEditing:NO animated:YES];
[self.myTableView setEditing:NO animated:YES];
EditButton.title = #"Edit";
cellText.hidden = YES; //<-- THIS IS THE CODE
}
else
{
[self setEditing:YES animated:YES];
[self.myTableView setEditing:YES animated:YES];
EditButton.title = #"Done";
cellText.hidden = NO; //<-- THIS IS THE CODE
}
}
but it only shows and hides the VERY LAST cell's textField. How can i get it to where it shows and then doesnt show EVERY cell's textFIeld? Many thanks in advance!!!
CELL FOR ROW
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cellText = [[UITextField alloc]init];
[cellText setFrame:CGRectMake(190, 15, 55, 30)];
cellText.text = #"1";
cellText.borderStyle = UITextBorderStyleRoundedRect;
cellText.hidden = YES;
cellText.userInteractionEnabled = NO;
[cell addSubview:cellText];
}
return cell;
}
Thanks in advance!! :D
You can get rid of this issue, using this trick, I am not sure as if it would create the memory leak within your code.Since, it create new cell each time.But surely you can use it,if you don't get some proper way of doing it. ;)
- (IBAction)editButton:(id)sender
{
if (self.editing)
{
[self setEditing:NO animated:YES];
[self.myTableView setEditing:NO animated:YES];
EditButton.title = #"Edit";
}
else
{
[self setEditing:YES animated:YES];
[self.myTableView setEditing:YES animated:YES];
EditButton.title = #"Done";
}
[self.myTableView reloadData];
}
After reloading the TableView, check the condition in cellForRowAtIndexPath, either pass the value of self.editing to TextField which makes it hide/show.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cellText = [[UITextField alloc]init];
[cellText setFrame:CGRectMake(190, 15, 55, 30)];
cellText.text = #"1";
cellText.borderStyle = UITextBorderStyleRoundedRect;
cellText.hidden = YES;
cellText.backgroundColor = [UIColor redColor];
cellText.userInteractionEnabled = NO;
[cell addSubview:cellText];
cellText.hidden=!self.editing;
return cell;
}
Although you create a text field for each cell, you keep a reference only to the last one in the ivar named cellText. That's why you show/hide the only text field.
I suggest you reload the table when toggling the editing mode, and set the text fields visibility in tableView:cellForRowAtIndexPath:.
Oh, and you should release cellText after adding it as a subview. Otherwise you are leaking memory. And it is highly recommended that you add subviews to UITableViewCell content view, rather than directly to the cell.
Try this code
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UITextField * cellText = [[UITextField alloc] initWithFrame:CGRectMake(1, 1, 100, 30)];
cellText.tag = 1;
cellText.textColor = [UIColor darkTextColor];
//cellText.numberOfLines = 0;
cellText.font = [ UIFont fontWithName: #"Helvetica-Bold" size: 12.0 ] ;
cellText.backgroundColor = [ UIColor clearColor ] ;
cellText.text = #"123";
cellText.hidden = YES;
[cell.contentView addSubview:cellText];
[cellText release];
cellText =nil;
// Set up the cell...
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
UITextField *cellText = (UITextField *)[[aTableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
if (!self.editing)
{
[self setEditing:NO animated:YES];
[self.tableView setEditing:NO animated:YES];
// EditButton.title = #"Edit";
cellText.hidden = YES; //<-- THIS IS THE CODE
}
else
{
[self setEditing:YES animated:YES];
[self.tableView setEditing:YES animated:YES];
// EditButton.title = #"Done";
cellText.hidden = NO; //<-- THIS IS THE CODE
}
return UITableViewCellEditingStyleNone;
}
hi friend this code is working fine for me, believe you too have a grate day
This is actually normal. Per the Apple documentation under addSubview:
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.
So it will keep removing it adding and removing from cells until it gets to the last one.
I defined my own controller with no nib file like this:
#interface EngineViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
EngineViewProperties* viewProp;
UIImageView *imgView; // image view of selected engine
NSUInteger selectedIndex;
UITableView *menu;
}
#property (nonatomic,retain) EngineViewProperties* viewProp;
- (EngineViewController *) initWithEngineViewProperties: (EngineViewProperties *) _viewProp;
- (void) dropdownMenu: (id) sender;
I created my view in loadView,with three subviews. The subview arrowBtn is helped to popup a list of search engines.
- (void)loadView {
// ...
UIButton *arrowBtn = [[UIButton alloc] initWithFrame:rect];
[arrowBtn setImage:viewProp.arrowImg forState:UIControlStateNormal];
[arrowBtn addTarget:self action:#selector(dropdownMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imgView];
[self.view addSubview:label];
[self.view addSubview:arrowBtn];
// ...
}
I create a table listing search engines in the selector dropdownMenu:
- (void) dropdownMenu: (id) sender {
UIButton *arrowBtn = (UIButton *)sender;
// ...
menu = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
menu.delegate = self;
menu.dataSource = self;
menu.backgroundColor = [UIColor blackColor];
menu.allowsSelection = YES;
[self.view addSubview:menu];
[self.view bringSubviewToFront:menu];
[menu release];
}
And I implemented
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
as usual.
But the result is that the menu popup happily,but I can do nothing with the cells.I clicked the cells,but no response.Those methods like "didSelectRowAtIndexPath" can not be called.
Sorry to paste up so much codes one time.But I really need help.I don't know what is the problem.Please forgive me for my poor English and low development skill in Iphone.And thanks a lot if you give me a little suggestions.
//Added-------------------------
"numberOfRowsInSection" is simple:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [viewProp.txtArray count];
}
and another method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MenuItems = #"MenuItems";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MenuItems];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: MenuItems] autorelease];
}
NSUInteger row = [indexPath row];
cell.imageView.image = [viewProp.imgArray objectAtIndex:row];
cell.textLabel.text = [viewProp.txtArray objectAtIndex:row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone; // Blue style tried,helpless too
if (row == selectedIndex) {
cell.selected = YES;
}
cell.userInteractionEnabled = YES;
return cell;
}
// Added
Strangely,when the menu firstly poped up,the default selected cell was highlighted correctly,but very quickly the highlighting disappeard.
Check if you have implemented this
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
The didSelectRowAtIndexPath will not called if this is present. One way to workaround this is set [self.view addGestureRecognizer:tap]; towards your targeted view only such as [self.svContent addGestureRecognizer:tap];.
Alternatively, check which view is touched.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ((touch.view == YourTable))
{
return NO;
}
return YES;
}
Try giving cell.selectionStyle = UITableViewCellSelectionStyleBlue in your table View data source. Also check if the userInteraction is enabled for the table view and the cells.
Are you showing any data in the table have you implemented
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
in you viewController??
Seems that you are setting the delgate property correctly, also you added the protocols to the same ViewController. So the problem can be either there is no data in the tableView for selection, or there is some View on top of tableView which is blocking you interaction with the tableView.
Use
cell.selectionStyle = UITableViewCellSelectionStyleGray;
in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for your cell.
remove
cell.userInteractionEnabled = YES;
code from your file.