我有从服务器获取数据的方法在表中使用的问题是,当获取数据时,如果数组中已有3个项目,然后在方法调用后变为5,我们重新加载数据则会生成重复的记录
[self saveData];
[self setUpData];
[tableView reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
int count=[resultArray count];
NSLog(@"resultArry Row Counts is %d",count);
return [resultArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.00;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCellF *cell = (CustomCellF *)[tableView
dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellF"
owner:self options:nil];
for(id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCellF class]])
cell = (CustomCellF *)oneObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
FeedbackData *theCellData = [resultArray objectAtIndex:indexPath.row];
cell.theTitle.text =theCellData.user_Feedback;
NSString*type=theCellData.user_Rating;
if ([type isEqualToString:@"One Star"]) {
cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage2.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage3.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
}
else if ([type isEqualToString:@"Two Stars"]) {
cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage3.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
}
else if ([type isEqualToString:@"Three Stars"]) {
cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage4.image=[UIImage imageNamed:@"stargray.png"];
cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
}
else if ([type isEqualToString:@"Four Stars"]) {
cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage4.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage5.image=[UIImage imageNamed:@"stargray.png"];
}
else {
cell.theCellImage1.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage2.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage3.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage4.image=[UIImage imageNamed:@"starblue.png"];
cell.theCellImage5.image=[UIImage imageNamed:@"starblue.png"];
}
return cell;
}
答案 0 :(得分:0)
如果你想重新填充你的阵列,首先让它们为零,以避免你现在面对的......
if(dataArray!=nil){
dataArray=nil;
dataArray=[[NSMutableData alloc]init];
}
答案 1 :(得分:0)
在调用方法(setUpData)时,您将获得两次相同的数据或添加新数据。
您需要它来删除所有旧数据并使用新数据集,为此您需要从数组中删除/释放所有数据并向其中添加新对象。
方法中的(如果使用ARC)
-(void) setUpData{
if(yourArray!=nil){
yourArray=nil;
yourArray=[NSMutableArray new];
//fill data again here
}
}
答案 2 :(得分:0)
如果你想重新填充数组,那么首先你必须做空。
if(resultArray.count >0)
{
[resultArray removeAllObjects];
}
然后重新填充数组。
答案 3 :(得分:0)
首先发布你的数组:
[dataArray release];
或者如果您使用Arc
那么
[dataArray removeAllObjects];
然后检查并再次填写
if(dataArray!=nil){
dataArray=nil;
dataArray=[[NSMutableData alloc]init];
}