Objective c 如何根据条件将数据发送到表视图
下面是我用来检查纬度和经度是否在100米半径范围内的代码Objective c 如何根据条件将数据发送到表视图,objective-c,uitableview,latitude-longitude,Objective C,Uitableview,Latitude Longitude,下面是我用来检查纬度和经度是否在100米半径范围内的代码 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //
- (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 [dataArray 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
Person *temp = (Person *)[self.persons objectAtIndex:indexPath.row];
cell.textLabel.text = temp.strName;
// Configure the cell...
return cell;
}
- (void) readDataFromDatabase{
sqlite3 *database;
persons = [[NSMutableArray alloc]init];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){
const char *sqlStatement = "select * from sdata";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
pID = sqlite3_column_int(compiledStatement, 0);
// NSLog(@" Id : %d",pID);
pName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSLog(@" Name : %@",pName);
pAdd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSLog(@" Address : %@",pAdd);
pPh = sqlite3_column_int(compiledStatement, 3);
NSLog(@" PhoneNo : %d",pPh);
pCp = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSLog(@" ContactPerson : %@",pCp);
pLat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSLog(@" Latitude : %@",pLat);
pLong = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSLog(@" Longitude : %@",pLong);
Person *temp = [[Person alloc]initWithID:pID name:pName address:pAdd phone:pPh contactperson:pCp fLat:pLat fLong:pLong];
[persons addObject:temp];
[temp release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
- (void)startTest
{
for(int j=0;j<[persons count];j++){
Person *arr = [persons objectAtIndex:j];
NSString *s = [[NSString alloc]initWithFormat:@"%@ %@ %@",arr.fLat,arr.fLong,arr.strName] ;
// NSLog(@" data in array : %@", s);
#define COORDS_COUNT 1
#define RADIUS 100.0f
CLLocationCoordinate2D coords[COORDS_COUNT] = {
CLLocationCoordinate2DMake([arr.fLat floatValue], [arr.fLong floatValue ]),
};
CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat floatValue], [longt floatValue]);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:RADIUS identifier:@"Banashankari"];
for (int i = 0; i < COORDS_COUNT; i++)
{
CLLocationCoordinate2D coord = coords[i];
NSString *coordString = [NSString stringWithFormat:@"%f,%f",coord.latitude, coord.longitude];
[dataArray addObject:coordString];
if ([region containsCoordinate:coord])
{
NSLog(@"location %f, %f is within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);
[resultArray addObject:coordString];
NSLog(@"data within 100 meters %@",resultArray);
}
else
{
NSLog(@"location %f, %f is not within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);
}
}
}
}
-(NSInteger)节数表视图:(UITableView*)表视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
返回[dataArray count];
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
}
Person*temp=(Person*)[self.persons对象索引:indexath.row];
cell.textlab.text=temp.strName;
//配置单元格。。。
返回单元;
}
-(void)readDataFromDatabase{
sqlite3*数据库;
persons=[[NSMutableArray alloc]init];
if(sqlite3_打开([databasePath UTF8String],&database)==SQLITE_正常){
const char*sqlStatement=“select*from sdata”;
sqlite3_stmt*编译语句;
if(sqlite3\u prepare\u v2(数据库,sqlStatement,-1,&compiledStatement,NULL)==SQLITE\u OK){
while(sqlite3_步骤(compiledStatement)==SQLITE_行){
pID=sqlite3\u column\u int(compiledStatement,0);
//NSLog(@“Id:%d”,pID);
pName=[NSString stringWithUTF8String:(char*)sqlite3\u column\u text(compiledStatement,1)];
NSLog(@“名称:%@”,pName);
pAdd=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,2)];
NSLog(@“地址:%@”,pAdd);
pPh=sqlite3\u column\u int(compiledStatement,3);
NSLog(@“电话号码:%d”,pPh);
pCp=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,4)];
NSLog(@“联系人:%@”,pCp);
pLat=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,5)];
NSLog(@“纬度:%@”,平台);
pLong=[nsstringwithutf8string:(char*)sqlite3\u column\u text(compiledStatement,6)];
NSLog(@“经度:%@”,pLong);
Person*temp=[[Person alloc]initWithID:pID name:pName地址:pAdd电话:pPh联系人:pCp平面:pLat fLong:pLong];
[人员添加对象:临时];
[临时释放];
}
}
sqlite3_最终确定(编译语句);
}
sqlite3_关闭(数据库);
}
-(无效)开始测试
{
对于(int j=0;j,在方法numberOfRowsInSection
中,您正在对数据数组进行计数,在方法中,您正在加载cellforrowsindexpath
Person *temp = (Person *)[self.persons objectAtIndex:indexPath.row];
cell.textLabel.text = temp.strName;
应该是
cell.textLabel.text = [dataarray objectAtIndex:indexPath.row];
尝试此操作。如果未解决,请告诉我。@Sarah请检查我已更新的代码。是否…使用resultArray而不是数据数组,并在[resultArray addObject:coordString]之后保留resultArray;由于未捕获的异常“NSRangeException”,它将引发以下异常终止应用程序,原因:'***-[NSMutableArray objectAtIndex:]:索引3超出范围[0..2]'在numberOfRowsInSection中也获取结果Ray显示了100米范围内的纬度和经度。现在我如何显示数据库中这些纬度和经度的相应名称。是的,我得到了解决方案,但我只需要显示纬度和经度的相应名称。自定义单元格表示sho我可以呼叫阵列吗?。