I have searched on this problem, but have found nothing. I have triple checked that myTableView is linked to the table and not nil at the time of calling reloadData, and that the new array I’m feeding the table is new and not nil.
Basically though, the code seems to work. It’s just that I need to close and reopen the view for the table to be updated. Otherwise when I click on a button that should update the table, instead it makes it empty (until I close and reopen the view — then I get the right data).
.H:
@interface AddressbookViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UIButton * A;
IBOutlet UIButton * B;
IBOutlet UIButton * C;
IBOutlet UIButton * D;
IBOutlet UIButton * E;
IBOutlet UIButton * F;
IBOutlet UIButton * G;
IBOutlet UIButton * H;
IBOutlet UIButton * I;
IBOutlet UIButton * J;
IBOutlet UIButton * K;
IBOutlet UIButton * L;
IBOutlet UIButton * M;
IBOutlet UIButton * N;
IBOutlet UIButton * O;
IBOutlet UIButton * P;
IBOutlet UIButton * Q;
IBOutlet UIButton * R;
IBOutlet UIButton * S;
IBOutlet UIButton * T;
IBOutlet UIButton * U;
IBOutlet UIButton * V;
IBOutlet UIButton * W;
IBOutlet UIButton * X;
IBOutlet UIButton * Y;
IBOutlet UIButton * Z;
IBOutlet UIButton * vcCorp;
IBOutlet UIButton * vcLocal;
IBOutlet UIButton * listUp;
IBOutlet UIButton * listDown;
IBOutlet UIButton * close;
IBOutlet UIView *myView;
IBOutlet UITableView * myTableView;
NSMutableArray *tableArray;
CrestronClient * cClient;
CrestronControllerValues * CCV;
int viewOptions;
}
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) IBOutlet UITableView *myTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil type:(int)viewoptions;
@end
.M
#import "AddressbookViewController.h"
#import "CrestronControllerValues.h"
@implementation AddressbookViewController
@synthesize myTableView,tableArray;
//viewOptions = 1=videoconferenceLOCAL 2=videoconferenceCORP 3=telepresence 4=audioconference
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil type:(int)viewoptions
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
viewOptions = 1;
viewOptions = viewoptions;
tableArray = [[NSMutableArray alloc]initWithObjects:nil];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
CCV = [CrestronControllerValues sharedManager];
cClient = [CrestronClient sharedManager];
[vcLocal setSelected:TRUE];
[vcCorp setSelected:FALSE];
if (viewOptions>2) {
A.hidden = true;
B.hidden = true;
C.hidden = true;
D.hidden = true;
E.hidden = true;
F.hidden = true;
G.hidden = true;
H.hidden = true;
I.hidden = true;
J.hidden = true;
K.hidden = true;
L.hidden = true;
M.hidden = true;
N.hidden = true;
O.hidden = true;
P.hidden = true;
Q.hidden = true;
R.hidden = true;
S.hidden = true;
T.hidden = true;
U.hidden = true;
V.hidden = true;
W.hidden = true;
X.hidden = true;
Y.hidden = true;
Z.hidden = true;
vcCorp.hidden = true;
vcLocal.hidden = true;
listUp.hidden = true;
listDown.hidden = true;
//[myTableView setEditing:YES animated:YES];
//[myTableView reloadData];
}
[A addTarget:self action:@selector(aPressed)
forControlEvents:UIControlEventTouchUpInside];
[B addTarget:self action:@selector(bPressed)
forControlEvents:UIControlEventTouchUpInside];
[C addTarget:self action:@selector(cPressed)
forControlEvents:UIControlEventTouchUpInside];
[D addTarget:self action:@selector(dPressed)
forControlEvents:UIControlEventTouchUpInside];
[E addTarget:self action:@selector(ePressed)
forControlEvents:UIControlEventTouchUpInside];
[F addTarget:self action:@selector(fPressed)
forControlEvents:UIControlEventTouchUpInside];
[G addTarget:self action:@selector(gPressed)
forControlEvents:UIControlEventTouchUpInside];
[H addTarget:self action:@selector(hPressed)
forControlEvents:UIControlEventTouchUpInside];
[I addTarget:self action:@selector(iPressed)
forControlEvents:UIControlEventTouchUpInside];
[J addTarget:self action:@selector(jPressed)
forControlEvents:UIControlEventTouchUpInside];
[K addTarget:self action:@selector(kPressed)
forControlEvents:UIControlEventTouchUpInside];
[L addTarget:self action:@selector(lPressed)
forControlEvents:UIControlEventTouchUpInside];
[M addTarget:self action:@selector(mPressed)
forControlEvents:UIControlEventTouchUpInside];
[N addTarget:self action:@selector(nPressed)
forControlEvents:UIControlEventTouchUpInside];
[O addTarget:self action:@selector(oPressed)
forControlEvents:UIControlEventTouchUpInside];
[P addTarget:self action:@selector(pPressed)
forControlEvents:UIControlEventTouchUpInside];
[Q addTarget:self action:@selector(qPressed)
forControlEvents:UIControlEventTouchUpInside];
[R addTarget:self action:@selector(rPressed)
forControlEvents:UIControlEventTouchUpInside];
[S addTarget:self action:@selector(sPressed)
forControlEvents:UIControlEventTouchUpInside];
[T addTarget:self action:@selector(tPressed)
forControlEvents:UIControlEventTouchUpInside];
[U addTarget:self action:@selector(uPressed)
forControlEvents:UIControlEventTouchUpInside];
[V addTarget:self action:@selector(vPressed)
forControlEvents:UIControlEventTouchUpInside];
[W addTarget:self action:@selector(wPressed)
forControlEvents:UIControlEventTouchUpInside];
[X addTarget:self action:@selector(xPressed)
forControlEvents:UIControlEventTouchUpInside];
[Y addTarget:self action:@selector(yPressed)
forControlEvents:UIControlEventTouchUpInside];
[Z addTarget:self action:@selector(zPressed)
forControlEvents:UIControlEventTouchUpInside];
[listUp addTarget:self action:@selector(listUpPressed)
forControlEvents:UIControlEventTouchUpInside];
[listDown addTarget:self action:@selector(listDownPressed)
forControlEvents:UIControlEventTouchUpInside];
[close addTarget:self action:@selector(closedPressed)
forControlEvents:UIControlEventTouchUpInside];
[vcCorp addTarget:self action:@selector(vcCorpPressed)
forControlEvents:UIControlEventTouchUpInside];
[vcLocal addTarget:self action:@selector(vcLocalPressed)
forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view from its nib.
switch (viewOptions) {
case 1:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
case 2:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
case 3:
[self setTableArray:CCV.currentPhonebookEntriesTelepresence];
break;
case 4:
[self setTableArray:CCV.currentPhonebookEntriesAudioChat];
break;
default:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int startCounter = 0;
switch (viewOptions) {
case 1:
startCounter = 308;
break;
case 2:
startCounter = 308;;
break;
case 3:
startCounter = 331;
break;
case 4:
startCounter = 286;
break;
default:
return nil;
break;
}
if ([[tableArray objectAtIndex:indexPath.row]isEqualToString:@"< empty >"]) {
NSLog(@"empty");
}
[cClient userDigitalPush:startCounter+indexPath.row];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableArray 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] autorelease];
}
cell.textLabel.text = [tableArray objectAtIndex:indexPath.row];
return cell;
}
-(void)setTableArray:(NSMutableArray *)tablearray{
[tableArray removeAllObjects];
tableArray = tablearray;
NSLog(@"testing %@",tableArray);
//[myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[self viewWillAppear:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self myTableView] reloadData];
}
-(void)aPressed
{
// CCV = [CrestronControllerValues sharedManager];
//
// NSLog(@"test %@", CCV.cameleonVersion);
// NSLog(@"test %@", CCV.ipAddress);
// NSLog(@"test %@", CCV.iPID);
}
-(void)bPressed
{
}
-(void)cPressed
{
}
-(void)dPressed
{
}
-(void)ePressed
{
}
-(void)fPressed
{
}
-(void)gPressed
{
}
-(void)hPressed
{
}
-(void)iPressed
{
}
-(void)jPressed
{
}
-(void)kPressed
{
}
-(void)lPressed
{
}
-(void)mPressed
{
}
-(void)nPressed
{
}
-(void)oPressed
{
}
-(void)pPressed
{
}
-(void)qPressed
{
}
-(void)rPressed
{
}
-(void)sPressed
{
}
-(void)tPressed
{
}
-(void)uPressed
{
}
-(void)vPressed
{
}
-(void)wPressed
{
}
-(void)xPressed
{
}
-(void)yPressed
{
}
-(void)zPressed
{
}
-(void)listUpPressed
{
//viewOptions = 1=videoconferenceLOCAL 2=videoconferenceCORP 3=telepresence 4=audioconference
int startCounter = 0;
switch (viewOptions) {
case 1:
startCounter = 318;
break;
case 2:
startCounter = 318;;
break;
case 3:
startCounter = 341;
break;
case 4:
startCounter = 0;
break;
default:
return nil;
break;
}
[cClient userDigitalPush:startCounter];
if (viewOptions == 1 || viewOptions == 2) {
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
}else if(viewOptions == 3){
[self setTableArray:CCV.currentPhonebookEntriesTelepresence];
}else if(viewOptions == 4){
[self setTableArray:CCV.currentPhonebookEntriesAudioChat];
}
}
-(void)listDownPressed
{
int startCounter = 0;
switch (viewOptions) {
case 1:
startCounter = 318;
break;
case 2:
startCounter = 318;;
break;
case 3:
startCounter = 341;
break;
case 4:
startCounter = 0;
break;
default:
return nil;
break;
}
[cClient userDigitalPush:startCounter+1];
if (viewOptions == 1 || viewOptions == 2) {
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
}else if(viewOptions == 3){
[self setTableArray:CCV.currentPhonebookEntriesTelepresence];
}else if(viewOptions == 4){
[self setTableArray:CCV.currentPhonebookEntriesAudioChat];
}
}
-(void)closedPressed
{
[self.view removeFromSuperview];
}
-(void)vcCorpPressed
{
[cClient userDigitalPush:326];
[vcLocal setSelected:FALSE];
[vcCorp setSelected:TRUE];
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
}
-(void)vcLocalPressed
{
[cClient userDigitalPush:325];
[vcLocal setSelected:TRUE];
[vcCorp setSelected:FALSE];
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
}
- (void)dealloc
{
[myTableView release];
[tableArray release];
[cClient release];
[super dealloc];
}
@end
from what i have found it seems like
[tableArray removeAllObjects];
might have been my problem. i thought it was a timing issue but even after adding a sleep(1) on a different thread my problem wasnt fixed.
here is the working code. notice i changed from remove all objects to removing each object individually.
this is the second time that removeallobjects hasnt worked how i expect it. im going to re-read the documentation but i really dont understand why that was the problem. i was clearing the array, setting it to a new array, and it would still be empty
and here is my answer