I have a storyboard with a UIView and a UITableView in it. The UITableView has custom cells (UITableViewCell with xib).
When I run the app, only the first row of the table is shown, the rest of the table is hidden.
The rows are not seen on the screen, but they respond to user interaction as expected.
The rest of the cells are revealed only after the table is scrolled upwards.
What could be the problem?
Here is the code for UIView with the table:
#implementation EldersTableViewController
#synthesize items;
#synthesize tableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
EldersDataHendler *edh = [[EldersDataHendler alloc]init];
NSMutableArray *itemsFromPList = [edh dataForTableFrom:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"eldersData" ofType:#"plist"]]];
self.items = itemsFromPList;
[edh release];
edh=nil;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (IBAction)didPressBackButton:(UIButton *)sender {
[self.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];
EldersItemCell* cell = (EldersItemCell*)[tableView dequeueReusableCellWithIdentifier:#"EldersItemCell"];
if (cell == nil) {
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:#"EldersItemCell" owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[EldersItemCell class]]){
cell = (EldersItemCell*)obj;
break;
}
}
}
[cell setObject:cellInfo];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SEObject *cellInfo = [self.items objectAtIndex:indexPath.row];
if (!eldersDetailsViewController){
eldersDetailsViewController = [[self.storyboard instantiateViewControllerWithIdentifier:#"elderDetailsVC"] retain];
}
eldersDetailsViewController.seObject = cellInfo;
[self.view addSubview:eldersDetailsViewController.view];
}
- (void)dealloc {
[eldersDetailsViewController release];
[tableView release];
[super dealloc];
}
#end
Thanks in advance,
Luda
1st just create an object class for your cell design,then import it in your .m file.then try like this and connect the table view delegate and data source properly
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Displayfriendscell";
Displayfriendscell *cell = (Displayfriendscell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[Displayfriendscell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
[[cell viewWithTag:121] removeFromSuperview];
[[cell viewWithTag:151] removeFromSuperview];
friend *user = [sortedFriendsArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.nameLabel.text = user.contactNAame;
cell.nameLabel.text = user.friendName;
//cell.nameLabel.text =[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:#"name"];
//user.friendName=[[sortedFriendsArray objectAtIndex:indexPath.row] objectForKey:#"name"];
NSLog(#"name is%#",cell.nameLabel.text);
return cell;
}
Problem solved! Thanks a lot dear arooaroo. There was something with the custom cell with the xib. So I've created a custom Table View Cell programmatically. Here is a nice tutorial
Related
i want to restore the check mark when my table view is reloaded when i click on the table view the check mark is added and when i clicked on the 2nd cell the old checked is removed that work fine now my question is that when my tableview is reloaded the old check mark should appear on the cell.
thanks
#import "mapMenuVC.h"
#import "ViewController.h"
#import "AppDelegate.h"
#interface mapMenuVC ()
#end
#implementation mapMenuVC
#synthesize checkedIndexPath;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
dataArray = [[NSMutableArray alloc]initWithObjects:#"Street Map",#"Satellite View",#"Hybird", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count];
}
- (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];
}
if([checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:16.0];
cell.textLabel.text = [dataArray objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
switch (indexPath.row) {
case 0: {
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeStandard];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
case 1:
{
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeSatellite];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
case 2:
{
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeHybrid];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
please write follow bellow steps
In TableViewDidSelectRowAtIndexPath
[[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:#"Checked"];
[[NSUserDefaults standardUserDefaults] synchronize];
Now In CellForRowAtIndexPath
if(indexPath.row == [[NSUserDefaults standardUserDefaults]integerForKey:#"Checked"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
It only CheckMarked the last value you have selected
You need to store the last checked row index so you can load that value the next time you bring up the view controller. Since you appear to only have one section you just need to save (and restore) the row index. NSUserDefaults is a good place to store this NSInteger value. You can save the row index each time a new selection is made. You can load the current value in the view controller's viewDidLoad method.
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *petsArray;
}
#property (nonatomic, strong) NSMutableArray *petsArray;
#end
RootViewController.m
#import "RootViewController.h"
#import "PetsViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
petsArray = [[NSMutableArray alloc] init];
[petsArray addObject:#"Dog"];
[petsArray addObject:#"Cat"];
[petsArray addObject:#"Snake"];
[self setTitle:#"PETS !"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [petsArray count];
}
- (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];
}
// Configure the cell...
cell.textLabel.text = [petsArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
I think that the problem is in didSelectRowAtIndexPath which I can't use in storyboarded project.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PetsViewController *pets = [[PetsViewController alloc] initWithNibName:#"PetsViewController" bundle:nil];
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Dog"]) {
pets.petsInt = 0;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Cat"]) {
pets.petsInt = 1;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
if ([[petsArray objectAtIndex:indexPath.row] isEqual:#"Snake"]) {
pets.petsInt = 2;
[pets setTitle:[petsArray objectAtIndex:indexPath.row]];
}
[self.navigationController pushViewController:pets animated:YES];
}
#end
PetsViewController.h
#import <UIKit/UIKit.h>
#interface PetsViewController : UITableViewController {
NSMutableArray *dogArray;
NSMutableArray *catArray;
NSMutableArray *snakeArray;
int petsInt;
}
#property int petsInt;
#end
PetsViewController.m
#import "PetsViewController.h"
#interface PetsViewController ()
#end
#implementation PetsViewController
#synthesize petsInt;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dogArray = [[NSMutableArray alloc] initWithObjects:#"HAF",#"hafo", #"hafinio", nil];
catArray = [[NSMutableArray alloc] initWithObjects:#"MYAU",#"myainio", #"mya lya lya", nil];
snakeArray = [[NSMutableArray alloc] initWithObjects:#"fshhhhh",#"fsssss", #"xrt", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (petsInt == 0) return [dogArray count];
if (petsInt == 1) return [catArray count];
if (petsInt == 2) return [snakeArray count];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PetsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (petsInt == 0) cell.textLabel.text = [dogArray objectAtIndex:indexPath.row];
if (petsInt == 1) cell.textLabel.text = [catArray objectAtIndex:indexPath.row];
if (petsInt == 2) cell.textLabel.text = [snakeArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
[self performSegueWithIdentifier:#"SEGUE_NAME" sender:self];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"SEGUE_NAME"]) {
PetsViewController *pets = [segue destinationViewController];
[pets setPetsInt:0];
[pets setTitle:#"Title"];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 0;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 1;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 2;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
if ([[gazel objectAtIndex:indexPath.row] isEqual:#"<էրեբունի> Օդանավակայան"]) {
KangarList *kang = [segue destinationViewController];
kang.kangarInt = 3;
[kang setTitle:[gazel objectAtIndex:indexPath.row]];
}
}
i have tried many ways and i finaly realized the right method, but it works only when under view did load method objects have only one description but if it hase something like thise
gazel = [[NSMutableArray alloc] init];
Data *data = [[Data alloc] init];
data.title = #"<էրեբունի> Օդանավակայան";
data.subtitle = #"Հարաֆ Արևմտյան Թաղամաս";
data.photo = 1;
[gazel addObject:data];
its not working
The below Code works but not as i wish.i want that when i click UIbutton its automaically update the new value in UITableview instead of old value.Below Code works only when i press the UIbuttons and after that when i scroll the UITableview then it update the UItableview with new values.
In my application i using UITableview as Subclass of my mainclass.as image show below
I add Tableview in my Mainclass which is "testingViewController" like this way
In testingViewController.h
#import "Inputtableview.h"
#interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
Inputtableview *inputview;
IBOutlet UITableView *inputtbl;
}
#end
In testingViewController.m
- (void)viewDidLoad {
btn1bool=FALSE;
if (inputview == nil) {
inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
}
Now in Button action method
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
}
my Subclass code "inputtableview.m" is show below
- (void)viewDidLoad {
[super viewDidLoad];
listOfItems=[[NSMutableArray alloc] initWithObjects:#"Iceland",#"Greenland",#"Switzerland",
#"Norway",#"New Zealand",#"Greece",#"Italy",#"Ireland",nil];
array1 = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H", nil] ;
}
#pragma mark -
#pragma mark Table View datasource methods
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
}
else {
return [listOfItems count];
}
[self.tableView reloadData];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"Row: %i", indexPath.row);
if (btn1bool) {
NSString *cellValue = [array1 objectAtIndex:indexPath.row];
cell.text = cellValue;
}
else {
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
}
return cell;
}
Any help will be appriated.
Just put the following code:
[inputtbl reloadData];
There are a few things you need to change in your project, but I assume this project is just for testing stuff.
You want the date to reload after you pressed the button, so you call the method in the IBAction.
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
[inputview.tableView reloadData];
}
To switch between the 2 data sources when the button is pressed you can change to this line of code: btn1bool=!btn1bool;
(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
} else {
return [listOfItems count];
}
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is correct
i want to attech UIActivityIndicator with UITableView when then first time table load on iphone activity must start blinking and when the table compleletely load in iphone when i click on load more cell then also i want to show activity blink for that moment when more rows loading kindly tell me how can i do that this is the code if you complete this code by your self then thanks in advance
#import "RootViewController.h"
#import "Tweet.h"
#implementation RootViewController
#synthesize customImage,pagecontrol,cell;//indexPath;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
pageSize = 2;
// totalpages =(NSInteger) (xmlParser.tweets)/5 ;
xmlParser = [[XMLParser alloc] loadXMLByURL:#"http://api.twitter.com/1/statuses/user_timeline/KentFranks.xml"];
[super viewDidLoad];
self.title = #"Tweets";
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([xmlParser.tweets count]>pageSize)
{
return pageSize+1;
}
return xmlParser.tweets.count;
}
- (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];
}
//UIImage *twitterLogo = [[UIImage imageNamed:#"twitter-logo.png"]autorelease];
NSString *imagepath = [[NSBundle mainBundle] pathForResource:#"twitter-logo" ofType:#"png"];
UIImage *image =[[ UIImage alloc] initWithContentsOfFile:imagepath];
cell.imageView.image = image;
cell.detailTextLabel.text= #"Add Subtitle here";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
if(indexPath.row<pageSize)
{
cell.textLabel.text = currentTweet.content;
}
else
{
cell.textLabel.text = #"Load more ";
}
NSLog(#"cell: %#",cell);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(IBAction)loadmorebutton:(id)sender;
{
NSIndexPath *currentPath = [self.tableView indexPathForSelectedRow];
NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row+1 inSection:currentPath.section];
[self.tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==pageSize)
{
pageSize=pageSize+2;
[tableView reloadData];
}
}
/*
-(IBAction)pagecontrol:(UIPageControl *)pageControl
{
[self.tableView reloadData];
}
-(IBAction)Pageindexchanges:(id)sender
{
NSLog(#"clicked page index: %i ",pagecontrol.currentPage);
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
#pragma mark -
#pragma mark Table view delegate
/*- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
*/
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
}
- (void)dealloc
{
[xmlParser release];
[super dealloc];
}
#end
Try this code may be helped you...
Add the spinner directly to the cell view felt a little hacky, so I used a 1x1 transparent png as the image view and resized it to be whatever my spinner size is:
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"NoReuse"] autorelease];
cell.textLabel.text = #"Loading...";
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
return cell;
That gives a cell that looks like this:
This May Help you
https://github.com/pothibo/PHRefreshTriggerView
And second one option is
https://github.com/enormego/EGOTableViewPullRefresh
I'm trying to be able to insert an object into my table, but it crashes because of an uncaught exception whenever I hit the add button. It says
Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update
Here's my main view controller which handles all my table stuff...
#import "MasterViewController.h"
#import "DetailViewController.h"
#define kFileName #"steakNames.plist"
#interface MasterViewController () {
NSMutableArray *_objects;
}
#end
#implementation MasterViewController
#synthesize steakName;
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFileName];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Master", #"Master");
}
return self;
}
- (void)viewDidLoad
{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
_objects = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)insertNewObject:(id)sender
{
if (!steakName) {
steakName = #"No name";
}
[_objects insertObject:[self steakName] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
// Customize the appearance of table view cells.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDate *object = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else 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 rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
NSDate *object = [_objects objectAtIndex:indexPath.row];
self.detailViewController.detailItem = object;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
[_objects writeToFile:[self dataFilePath] atomically:YES];
}
#end
Most likely the cause of the error is that this returns 0:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ...;
}
I would suggest that you first take a complete working example of table view with all its required methods and start from there.
Generally you need to implement these methods:
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
So far there is no issue with any NSMutableArray, anyway you cannot insert an object at an index if the array does not yet have the number of objects, eg insert at index 4 but the array has only 2 objects. You can always add objects to NSMutableArray and increase the number of objects in this array.