ios - SIGABRT accessing NSArray with indexPath.row -
i working on app game server company , part of app requires user see list of or game servers , whether or not online, offline, how many players on them, server name, etc. data found in php file hosted on web updated mysql database when viewed, outputs json.
using code below, doesn't seem work. load view , right away "thread 1: signal sigabrt" error on line nsdictionary *myserver = [servers objectatindex:indexpath.row];
. when removing indexpath.row
, replacing either 0 or 1, data displayed on uitableview
in storyboard, except displayed 4 times in row , entry in json file (either 0 or 1). can't keep @ fixed number client might have 100 servers, or 5 servers why need indexpath.row
. below, attached json looks when given server , accessed directly app's code
i'd appreciate if please let me know problem , propose solution unique situation rid of sigabrt error , once do, make sure doesn't show 4 times in tableview now.
my header file:
#import <uikit/uikit.h> #import "serverdetailviewcontroller.h" @interface secondviewcontroller : uitableviewcontroller { iboutlet uitableview *maintableview; nsdictionary *news; nsmutabledata *data; } @property (weak, nonatomic) iboutlet uibarbuttonitem *refreshserversbutton; - (ibaction)refreshservers:(id)sender; @end
my main file:
#import "secondviewcontroller.h" @interface secondviewcontroller () @end @implementation secondviewcontroller - (void)viewdidload { [super viewdidload]; [uiapplication sharedapplication].networkactivityindicatorvisible = yes; nsurl *url = [nsurl urlwithstring:@"redacted"]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [[nsurlconnection alloc] initwithrequest:request delegate:self]; } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { data = [[nsmutabledata alloc] init]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)thedata { [data appenddata:thedata]; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { [uiapplication sharedapplication].networkactivityindicatorvisible = no; news = [nsjsonserialization jsonobjectwithdata:data options:nil error:nil]; [maintableview reloaddata]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { uialertview *errorview = [[uialertview alloc] initwithtitle:@"error" message:@"unable load server list. make sure connect either 3g or wi-fi or try again later." delegate:nil cancelbuttontitle:@"dismiss" otherbuttontitles:nil, nil]; [errorview show]; [uiapplication sharedapplication].networkactivityindicatorvisible = no; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } - (int)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (int)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [news count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uicolor *colorgreen = [uicolor colorwithred:91.0f/255.0f green:170.0f/255.0f blue:101.0f/255.0f alpha:1.0f]; uicolor *colorred = [uicolor redcolor]; static nsstring *cellidentifier = @"maincell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } uilabel *servername = (uilabel *)[cell viewwithtag:100]; uilabel *serverplayers = (uilabel *)[cell viewwithtag:101]; uilabel *serverstatus = (uilabel *)[cell viewwithtag:102]; uilabel *serverofflinename = (uilabel *)[cell viewwithtag:103]; serverplayers.textcolor = [uicolor graycolor]; nsdictionary *resultdict = [news objectforkey:@"result"]; nsarray *servers = [resultdict objectforkey:@"servers"]; nsdictionary *myserver = [servers objectatindex:indexpath.row]; nsstring *titleofserver = [myserver objectforkey:@"title"]; nsnumber *statusofserver = [nsnumber numberwithint:[[myserver objectforkey:@"status"] intvalue]]; nsnumber *playersonserver = [nsnumber numberwithint:[[myserver objectforkey:@"players"] intvalue]]; if ([[statusofserver stringvalue] isequaltostring:@"0"]) { servername.text = @""; serverofflinename.text = titleofserver; serverstatus.textcolor = colorred; serverstatus.text = @"offline"; serverplayers.text = @""; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } else if ([[statusofserver stringvalue] isequaltostring:@"1"]) { servername.text = titleofserver; serverofflinename.text = @""; serverstatus.textcolor = colorgreen; serverstatus.text = @"online"; serverplayers.text = [playersonserver stringvalue]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } else if ([[statusofserver stringvalue] isequaltostring:@"2"]) { servername.text = @""; serverofflinename.text = titleofserver; serverstatus.textcolor = [uicolor bluecolor]; serverstatus.text = @"busy"; serverplayers.text = @""; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } else if ([[statusofserver stringvalue] isequaltostring:@"3"]) { servername.text = @""; serverofflinename.text = titleofserver; serverstatus.textcolor = [uicolor graycolor]; serverstatus.text = @"suspended"; serverplayers.text = @""; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } else if ([[statusofserver stringvalue] isequaltostring:@"-1"]) { servername.text = @""; serverofflinename.text = titleofserver; serverstatus.textcolor = [uicolor orangecolor]; serverstatus.text = @"critical error"; serverplayers.text = @""; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { serverdetailviewcontroller *detail = [self.storyboard instantiateviewcontrollerwithidentifier:@"detail"]; [self.navigationcontroller pushviewcontroller:detail animated:yes]; } - (ibaction)refreshservers:(id)sender { [uiapplication sharedapplication].networkactivityindicatorvisible = yes; nsurl *url = [nsurl urlwithstring:@"redacted"]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [[nsurlconnection alloc] initwithrequest:request delegate:self]; } @end
json code server: {"status":"ok","error":"","debug":"2 server(s)","result":{"servers":[{"id":1,"title":"test","players":0,"slots":10,"status":3},{"id":2,"title":"creative spawn","players":0,"slots":5,"status":-1}]}}
from code, looks source of error.(however, didn't read whole thing.)
- (int)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [news count]; //counted number of items in whole json object. }
and in cellforrowatindexpath:(nsindexpath *)indexpath
nsdictionary *resultdict = [news objectforkey:@"result"]; nsarray *servers = [resultdict objectforkey:@"servers"]; // used different array(an item of whole json array). // since news object has more items servers, caused out of bound here. nsdictionary *myserver = [servers objectatindex:indexpath.row];
try doing following code
- (int)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nsdictionary *resultdict = [news objectforkey:@"result"]; nsarray *servers = [resultdict objectforkey:@"servers"]; return [servers count]; //counted number of items in whole json object. }
Comments
Post a Comment