Setting reuseIdentifier on storyboard causes custom UITableViewCell to not be displayed - objective-c

When attempting to load a custom UITableViewCell from a nib, I am unable to get the cell to appear only if I have set the reuseIdentifier on the prototype cell in the main storyboard. If this is set to blank, I get a warning from xcode, however the code runs correctly. I have similar code in several other controllers, however this is the only one presenting this issue.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"AssetsTableCell";
AssetsTableCell *assetTableCell = (AssetsTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
int rightIndex = (indexPath.row * 4);
int rightMiddleIndex = (indexPath.row * 4) + 1;
int leftMiddleIndex = (indexPath.row * 4) + 2;
int leftIndex = (indexPath.row * 4) + 3;
if (assetTableCell == nil) {
NSArray *topLevelObjects;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AssetsTableCell_iPhone" owner:nil options:nil];
assetTableCell = (AssetsTableCell *)[topLevelObjects objectAtIndex:0];
[assetTableCell setDelegate:self];
}
// Need to collect the badge details in groups of 4.
#try {
NSArray *rowArray = [NSArray arrayWithObjects:[remoteContainers objectAtIndex:rightIndex],
[remoteContainers objectAtIndex:rightMiddleIndex],
[remoteContainers objectAtIndex:leftMiddleIndex],
[remoteContainers objectAtIndex:leftIndex],
nil];
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:
[rowArray objectAtIndex:0], #"LEFT_CONTAINER",
[rowArray objectAtIndex:1], #"LEFT_MIDDLE_CONTAINER",
[rowArray objectAtIndex:2], #"RIGHT_MIDDLE_CONTAINER",
[rowArray objectAtIndex:3], #"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
#catch (NSException *exception) {
DLog(#"Exceeds boundary of remoteContainers array by %d.", ([remoteContainers count] % 4));
int remainder = [remoteContainers count] % 4;
switch (remainder) {
case 1: {
// Check to see the number of remaining containers left in the rest of the array. This dictates the way the final row must be constructed.
if ([remoteContainers objectAtIndex:currentStartRangeIndex]) {
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], #"LEFT_CONTAINER", #"EMPTY_CELL", #"LEFT_MIDDLE_CONTAINER", #"EMPTY_CELL", #"RIGHT_MIDDLE_CONTAINER", #"EMPTY_CELL", #"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
}
break;
case 2: {
// There are two elements at the end of the array.
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], #"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], #"LEFT_MIDDLE_CONTAINER", #"EMPTY_CELL", #"RIGHT_MIDDLE_CONTAINER", #"EMPTY_CELL", #"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
break;
case 3: {
// There are three elements at the end of the array.
NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], #"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], #"LEFT_MIDDLE_CONTAINER", [remoteContainers objectAtIndex:leftMiddleIndex], #"RIGHT_MIDDLE_CONTAINER", #"EMPTY_CELL", #"RIGHT_CONTAINER", nil];
[assetTableCell populateCellData:cellData];
}
break;
default: {
DLog(#"Thought the array had more elements, however was unable to determine the number of elements remaining in array.");
}
break;
}
}
}
return assetTableCell;
}

It looks like you configure each cell only in the case where the tableView cannot dequeue a cell for you. For one thing, your cell reuse is probably broken as a result. So that's one issue.
And then in the case of the storyboard, if the cell identifier is set correctly then the [tableview dequeueReusableCellWithIdentifier:] will always return a cell for you. So assetTableCell will not be nil, even the first time.
It's not clear exactly what all your logic is here, but you need to be able to properly set the content of your cell in the case where assetTableCell is not nil. (either because the cell is being recycled or because the cell came from the storyboard cache)

Related

function to check if there is any empty cell in a multicolumn nstableview not working

Hi i have written a function to check if there is any empty cell in a multicolumn nstableview like below.However this function is not working and always checks for the columns in the first row only.. any suggestions.and what is the right way to check for it
-(BOOL)isTableRowsContainingEmptyCells
{
for(int row=0;row< [[self.myarray arrangedObjects] count];row++)
{
NSTableColumn *column1 = [self.formFieldValues
tableColumnWithIdentifier:#"A"];
NSTableColumn *column2 = [self.formFieldValues
tableColumnWithIdentifier:#"B"];
NSCell *cell1 = [column1 dataCellForRow:row];
NSCell *cell2 = [column2 dataCellForRow:row];
NSLog(#"cell1 %# cell2 %#",cell1,cell2);
if([[cell1 stringValue] isEqualToString:#""]||[cell1 stringValue]==nil||[[cell2 stringValue] isEqualToString:#""]||[cell2 stringValue]==nil)
{
return YES;
}
}
return NO;
}
Try like this
NSCell *cll=[tableView preparedCellAtColumn:0 row:0];
NSString *str=[cll stringValue];
NSInteger len=[str length];
if (len ==0)
{
NSLog(#"NO data")
}

Checking/Unchecking a tableviewcell with sections

Previously, I had a large-ish dataset (~530 records) being displayed in a tableview. The data was held in an array of dictionaries with two keys, ID and name. Previously, I tapped a cell, it added a check mark, and it sent the cell row number to a 'selectedArray'. As I had already alphabetically sorted them (still in one section), the indexPath.row which was stored in the selectedArray corresponded to the dictionaries' array index, so I could pull data (the ID) from the record.
I decided that I would split this into headers by alphabetical order (which was an absolute pain, I don't see why it's such a complex process to insert headers into a list of records). But now, as I only used indexPath.row, when I tick the first one, it ticks the first record of each section, and only returns the number 0, so I only get the first record in the whole dataset. Is there a simple way to correct this? Really appreciate any help.
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"inviteCell"];
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
if ([checkedCells objectForKey:[NSNumber numberWithInt:indexPath.row]] != nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// NSDictionary *friend = [sortedFriendsArray objectAtIndex:indexPath.row];
//---get the letter in the current section---
NSString *alphabet = [nameIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"SELF.name beginswith[c] %#", alphabet];
NSArray *states = [sortedFriendsArray filteredArrayUsingPredicate:predicate];
if ([states count]>0) {
//---extract the relevant state from the states object---
NSDictionary *friend = [states objectAtIndex:indexPath.row];
long long fbid = [[friend objectForKey:#"id"]longLongValue];
NSString *name = [friend objectForKey:#"name"];
NSString *urlString = [NSString stringWithFormat:#"https://graph.facebook.com/%qi/picture?type=square",fbid];
NSURL *url = [NSURL URLWithString:urlString];
UILabel *eventNameLabel = (UILabel *) [cell viewWithTag:1];
eventNameLabel.text = name;
UIImageView *eventLogo = (UIImageView*) [cell viewWithTag:2];
eventLogo.image = [UIImage imageNamed:#"112-group.png"];
// eventLogo.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];;
}
return cell;
}
CURRENT didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSNumber *indexNumber = [NSNumber numberWithInt:indexPath.row];
NSDictionary *friend = [sortedFriendsArray objectAtIndex:indexPath.row];
long long fbid = [[friend objectForKey:#"id"]longLongValue];
NSNumber *fbidNum = [NSNumber numberWithLongLong:fbid];
if ([checkedCells objectForKey:indexNumber] != nil) {
[checkedCells removeObjectForKey:indexNumber];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[checkedCells setObject:fbidNum forKey:indexNumber];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"Cell Pressed: %#",indexNumber);
NSLog(#"FBID: %lld",fbid);
NSLog(#"Array: %#",checkedCells);
}
It looks like you're only saving the checked cells by row, not by section and row. At minimum, don't you need to be testing the section as well as the row in this block of code?
if ([checkedCells objectForKey:[NSNumber numberWithInt:indexPath.row]] != nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
It's late, and I don't have my Mac handy to try this code, so maybe I'm missing something obvious. I'm not sure what you're talking about only returning zero ... is that indexPath.row?
EDIT:
To account for the section in your data array, I'd suggest storing the data as a dictionary of arrays, one dictionary entry keyed by each letter, with the inner array holding all of the entries starting with that letter. It's a little more complicated to retrieve the data, but it correctly accounts for the section.
I supposed you could create some kind of an offset to account for the number of entries in each section and then use that to index into a flat array, but that would be a lot harder to maintain, in my view. I think the dictionary of arrays is the way to go.

cellForRowAtIndexPath getting the right data under each section

I'm stuck with a problem populating an UITableView.
I have got an NSMutableArray with customers. It looks like this:
customer
first letter="A"
customer name="Adwars Inc."
customer
first letter="A"
customer name="Amman Co."
customer
first letter="B"
customer name="Building Inc."
customer
first letter="C"
customer name="Computer Co."
So I've got an object customer, which separates me each customer. And i've got some keys for each object.
In my second NSArray i've got all my first letters, which appear in my customer data. It look like this:
A
B
C
D
G
J
M
S
Z
I was able to get my right section count and rows in section, but when i try to populate my table view it always look like this:
SCREENSHOT
HERE IS MY CODE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"CustomerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
for(int i = 0; i < [firstletters count]; i++)
{
if (indexPath.section == i) {
for(int count = 0 ;count < [customers count]; count++)
{
NSString *firstletter;
NSString *key;
key = [firstletters objectAtIndex:indexPath.section];
firstletter = [[customers objectAtIndex:count] objectForKey: #"FirstLetter"];
if ([key isEqualToString:firstletter]) {
cell.textLabel.text = [[customers objectAtIndex:count] objectForKey: #"S_NAME1"];
cell.detailTextLabel.text = [[customers objectAtIndex:count] objectForKey: #"s_town"];
}
}
}
}
return cell;
}
What do i have to do to make it work?
I know you have already accepted an answer, but I just wanted to offer another thought on how this might be done with data structured in a different way. If you had a dictionary where the keys were the first letter of your customers' names, and the values were the customer objects whose first letter was the same as the key, then you wouldn't have to do any looping (I don't know if you're still doing that in your solution). I made an example project (to see if this would work) that structures the data this way, except that my "objects" are just the names of companies rather than customer objects. In my table view controller I have:
- (void)viewDidLoad {
[super viewDidLoad];
self.companyDict = [NSMutableDictionary dictionary];
NSArray *aArray = [NSArray arrayWithObjects:#"Abercrombie & Fitch",#"Altera",#"Agilent",#"Allelix",#"Abbott Laboratories", nil];
NSArray *cArray = [NSArray arrayWithObjects:#"CocaCola",#"Continental",#"ConocoPhillips", nil];
NSArray *mArray = [NSArray arrayWithObjects:#"Myriad Genetics",#"Myrexis",#"Microsoft",#"McDonald's", nil];
NSArray *nArray = [NSArray arrayWithObjects:#"Nokia",#"NPS Pharmaceuticals",#"Norelco",#"Netflix",#"Nextel",#"Navistar International", nil];
[self.companyDict setValue:aArray forKey:#"A"];
[self.companyDict setValue:cArray forKey:#"C"];
[self.companyDict setValue:mArray forKey:#"M"];
[self.companyDict setValue:nArray forKey:#"N"];
self.keys = [[self.companyDict allKeys] sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.keys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.companyDict valueForKey:[self.keys objectAtIndex:section]]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];
}
NSArray *theArray = [self.companyDict valueForKey:[self.keys objectAtIndex:indexPath.section]];
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
return cell;
}
For every cell, you're iterating through all the customers, repeatedly setting (and resetting) the textLabel and detailTextLabel for every customer whose first letter matches the current section (but you're not considering whether the index of that customer in that section matches the current indexPath.row or not). This means that in your code every cell will have the textLabel and detailTextLabel for the last customer whose first letter matches the current section's first letter.
Try this TableKit library.
This case the solution will be clean and elegant:
NSMutableDictionary* sectionMap = [NSMutableDictionary dictionaryWithCapacity:30];
for(NSDictionary* c in customers) {
NSString* firstLetter = [c objectForKey:#"FirstLetter"];
NSString* name = [c objectForKey:#"S_NAME1"];
NSString* town = [c objectForKey:#"s_town"];
TKSection* section = [sectionMap objectForKey:firstLetter];
if(!section) {
section = [TKSection sectionWithCells:nil];
section.headerTitle = firstLetter;
[sectionMap setObject:section forKey:firstLetter];
}
TKCell* cell = [TKStaticCell cellWithStyle:UITableViewCellStyleSubtitle text:name detailText:town];
[section addCell:cell];
}
self.sections = [sectionMap allValues];
TRY THIS (Copy and paste this code):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"CustomerCell"] autorelease];
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
}
for(int i = 0; i < [firstletters count]; i++) {
if (indexPath.section == i) {
for(int count = 0 ;count < [customers count]; count++) {
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
NSString *firstletter = nil;
NSString *key = nil;
key = [firstletters objectAtIndex:indexPath.section];
firstletter = [[customers objectAtIndex:count] objectForKey: #"FirstLetter"];
if ([key isEqualToString:firstletter]) {
cell.textLabel.text = [[customers objectAtIndex:count] objectForKey: #"S_NAME1"];
cell.detailTextLabel.text = [[customers objectAtIndex:count] objectForKey: #"s_town"];
}
}
}
}
return cell;
}

cancel button on search bar does not cancel correctly

I have a search bar, i can search now, but when I enter a text to search, and click the cancel button. It does not give me back my first stage, meaning full of the items in the table.
For example: I search the item with word: a, it gives me all the a items, yes, it is right now, but when i hit the cancel button, i want the programme gives me all the items exist, not just a items.
Here is the code: please help me out. Thank you so much.
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
searchBar.text = #"";
[searchBar resignFirstResponder];
letUserSelectRow = YES;
searching = NO;
self.tableView.scrollEnabled = YES;
NSLog(#"what text after cancel now: %#", searchBar.text);
[self.tableView reloadData];
}
- (NSMutableArray *) searchTableView {
NSString *searchText = searchBar.text;
NSLog(#"search text: %#", searchText);
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArr = [[NSMutableArray alloc] init];
for (NSDictionary *dTemp in arrayData)
{
NSString *tempStr = [dTemp objectForKey:#"url"];
NSLog(#"sTemp string: %#",[ NSString stringWithFormat:#"%#", tempStr]);
NSRange titleResultsRange = [tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
NSLog(#"1 count :%d", [resultArray count]);
[resultArray addObject:dTemp];
NSLog(#"2 count :%d", [resultArray count]);
[tempArr addObject:resultArray];
[resultArray release];
resultArray = [NSMutableArray new];
}
}
if (resultArray != nil) {
[resultArray release];
}
return tempArr;
}
- (void)searchBar:(UISearchBar *)aSearchBar textDidChange:(NSString *)searchText
{
NSLog(#"what text after cancel now: %#", searchBar.text);
if([searchText length] > 0) {
[sortedArray removeAllObjects];
searching = YES;
letUserSelectRow = YES;
self.tableView.scrollEnabled = YES;
NSMutableArray *searchArray = [self searchTableView];
sortedArray = [[NSMutableArray alloc] initWithArray:searchArray copyItems:YES];
for (int i = 0; i<[sortedArray count]; i++) {
NSLog(#"this is the search array: %#", [[sortedArray objectAtIndex:i] class]);
}
NSLog(#"sorted array: %d", [sortedArray count]);
}
else {
searching = NO;
letUserSelectRow = NO;
self.tableView.scrollEnabled = NO;
}
[self.tableView reloadData];
}
You don't need to override any of UISearchBar methods to accomplish this. The new way of doing this relies on the UISearchDisplay controller instead (specifically on shouldReloadTableForSearchString).
Declare your view controller to conform to UISearchDisplayDelegate protocol, and keep two instance variables: your model as NSArray (all data) and a filtered array as NSMutableArray (a subset of your data). The code you presently have in "searchTableView" would filter the content of the model and place it into the filtered NSMutableArray. Then you would override the following UITableView methods: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. In each, before returning, make a comparison to determine whether your tableView argument is equal to self.searchDisplayController.searchResultsTableView. If it is, the user is looking at the filtered list and your should use the content of the filtered NSMutableArray to create the view, otherwise, the user is looking at the whole data set and you should use the content of the NSArray that holds your model. Take a look at the following Apple code for a simple example of what I described:
http://developer.apple.com/library/ios/#samplecode/TableSearch/Introduction/Intro.html

Read plist data into a NSArray but get null

I got same warning here “local declaration hides instance variable” warning
but I got more problems...
Here is my code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.someaddress.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSLog(#"\n\nCONNECTION: %#", theConnection);
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSMutableArray *plist = [[NSMutableArray alloc] init];
plist = [listFile propertyList];
NSLog( #"\n 1111 plist is \n%#", plist );
//I can get a plist format data here,But nothing in 2222
NSLog(#"Now you see me tableView Row Count");
NSLog(#"TOTAL PLIST ROW COUNT IS = %i", [plist count]);
// Return the number of rows in the section.
return [plist count];
}
and I got Warning here"Local declaration of 'plist' hides instance variable"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"LightCell";
LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
NSLog(#"Now you see me Load Data %i", indexPath.row);
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//I try to get list data here But RETURN NULL
NSLog( #"\n 2222 plist is \n %#", plist);
switch (indexPath.row) {
case 0:
if ([plist valueForKey:#"nodeStatus"] == 0){
cell.lightImageView.image = [UIImage imageNamed:#"lightOff.png"];
NSLog(#"value for key Node Status : %#" ,[self.plists Valuefokey:#"nodeStatus"]);
//also return NULL !!
}
else if([self valueForKey:#"nodeStatus"] == 1){
cell.lightImageView.image = [UIImage imageNamed:#"lightOn.png"];
}
break;
case 1:
cell.lightLocation.text =[plist valueForKey:#"nodeName"] ;
if ([plist valueForKey:#"nodeStatus"] == 0){
cell.lightImageView.image = [UIImage imageNamed:#"lightOff.png"];
}
else if([plist valueForKey:#"nodeStatus"] == 1){
cell.lightImageView.image = [UIImage imageNamed:#"lightOn.png"];
};
break;
default:
break;
}
return cell;
}
This is the tow items I create in a plist
{
category = Light;
nodeID = 1;
nodeName = "Living Room";
nodeStatus = 0;
nodeTrigger = 0;
nodeType = "light_sw";
},
{
category = Light;
nodeID = 2;
nodeName = Kitchen;
nodeStatus = 0;
nodeTrigger = 0;
nodeType = "light_sw";
}
So that's my question ,Why can't I pass "plist" from
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
...
}
to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
and I use NSMutableArray *plist = [[NSMutableArray alloc] init];
But still appear "Local declaration of 'plist' hides instance variable"
???
hope someone can figure out this problem
Best Regards !
and I got Warning here"Local declaration of 'plist' hides instance variable"
Well, then, you should fix that.
The warning is telling you that you've declared two variables named plist: One local to this instance method, and the other an instance variable. The local variable, having a narrower scope, hides the instance variable, so that when you refer to plist in the method, you are referring to the local variable. This means that you cannot access anything stored in the instance variable by another method, nor store anything in it for another method to retrieve.
The solution is either to kill off or to rename the local variable. If the latter is what you want, use Xcode's “Edit All in Scope” feature.
Also:
NSMutableArray *plist = [[NSMutableArray alloc] init];
plist = [listFile propertyList];
Creating the array on the first of those lines is redundant, because you immediately replace your pointer to that array with the pointer to another array, returned by propertyList. Thus, you never use and you leak the first array. You should at least cut out the creation of the first array, and you should probably cut out the entire first line (thereby cutting out both the first array and the local variable).
Here is the code I fix the warning ,the program can build without any warning
it also can display the result after reading the plist in tableview
1.Load the plist:
- (void)viewDidLoad {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www. someaddress.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
plist = [listFile propertyList];
}
2.return the number to rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [plist count];
}
3.read the plist data to show result in cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"LightCell0";
LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
int i;
for (i=0; i<[plist count]; i++) {
//Get nodeName
if(indexPath.row == i)
{
cell.lightLocation.text = [[[plist objectAtIndex:i] valueForKey: #"nodeName"]description];
//Get Light Status to show the image
if ([[[plist objectAtIndex:i] valueForKey: #"nodeStatus"] intValue] == 0){
cell.lightImageView.image = [UIImage imageNamed:#"lightOff.png"];
}
else if([[[plist objectAtIndex:i] valueForKey: #"nodeStatus"] intValue] == 1){
cell.lightImageView.image = [UIImage imageNamed:#"lightOn.png"];
cell.lightSwitch.on=YES;
}
}
}
return cell;
}
It can get the right data ,and display the correct result in the tableview cells
BUTTTTTTT
If you scroll up the tableview,it's ok,when you on the top it will scroll down Automatically
When you "scroll down" the tableview,program crash ???
WHY ??? did I write anything wrong ???
plist = [listFile propertyList];=====>self.plist = [listFile propertyList];
THAT IS CORRECT