加载后,通过调用Objective-C中的其他服务更改表视图单元格中的文本值



在调用服务时,我已经加载了带有Name、Country和Id的表视图,其中包含以下响应。

(
{
Name = David;
Country = USA
Id = 100;
},
{
Name = Mike;
Id = 101;
Country = Australia
},
{
Name = John;
Id = 102;
Country = UK
}
)

我有一个关于的问题,我在表视图页脚中有一个按钮。当按下按钮时,调用服务只替换表视图中的Name,而不替换Country和Id,下面是按下按钮时我得到的响应。

(
{
Name = Phillip;
Id = 200;
},
{
Name = Jackman;
Id = 201;
},
{
Name = Arrow;
Id = 202;
}
)

以下是我尝试过的。但无法替换名称

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CartItemCell *cell = (CartItemCell *) [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CartItemCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell =  (CartItemCell *) currentObject;
cell.backgroundColor=[UIColor clearColor];
}
}
}
NSMutableDictionary *detailsdict=[cartArray objectAtIndex:indexPath.row];
cell.lblName.text = [detailsdict objectForKey:@"Name"];
cell.lblCountry.text = [detailsdict objectForKey:@"Country"];
cell.lblId.text = [detailsdict objectForKey:@"Id"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}

你能帮我解决这个问题吗。TIA-

按下按钮时,您将使用以下代码获取新数据,并在此数组中assine所有数据arrUserList

NSMutableArray *arrUserList = [[NSMutableArray alloc]init];
for (int i=0; i<[arrUserList count]; i++) 
{
NSString *strUserName = [[arrUserList objectAtIndex:i] valueForKey:@"Name"];
for (int j=0; j<[cartArray count]; j++) 
{
if (i==j) {
[[cartArray objectAtIndex:j] setValue:strUserName forKey:@"Name"];
}
}
}

我们不能直接从Dictionary的数组中更新Dictionary值。所以我们可以创建一个临时字典,保存所有更新的值,然后用同一索引中的旧值替换它。并在最后重新加载表数据。

for (int newIndex = 0 ; newIndex < arrNewUserList.count ; newIndex++) {
for (int oldIndex = 0 ; oldIndex < arrUserList.count ; oldIndex++) {
if(arrNewUserList[newIndex]["Id"] == arrUserList[oldIndex]["Id"]) {
NSMutableDictionary *dict = [NSMutableDictionary alloc] init];
dict = arrUserList[oldIndex];
dict["name"] = arrNewUserList[newIndex]["name"];         
[arrUserList replaceObjectAtIndex:oldIndex withObject:dict];
break;
}
}
}
[tableview reloadData];

最新更新