1
我有一個全局聲明的數組,並在表視圖中加載該數組,但是當我去其他控制器和更新該數組並重新加載表視圖再在表視圖中的標籤中的文本單元格覆蓋。較新的更新數據覆蓋並且以前也以相同的標籤顯示。這裏是我的代碼:其中項目數組是全局聲明數組。在tableview標籤中的以前的文本顯示後重新加載tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:@"Arial" size:16];
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"];
[cell addSubview:nameLabel];
[nameLabel release];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal];
[cell addSubview:deleteButton];
UILabel *priceMark = [[UILabel alloc]initWithFrame:CGRectMake(150, 8, 65, 25)];
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:@"Arial" size:14];
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray objectAtIndex:indexPath.row]
objectForKey:@"amount"]];
[cell addSubview:priceMark];
[priceMark release];
UILabel *qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(225, 8, 60, 25)];
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"];
[cell addSubview:qtyLabel];
[qtyLabel release];
return cell;
}
EDIT 2
現在我得到另一個問題,當我更新是全球在0指數聲明項數組,並返回然後它不會在索引0更新表視圖鱗次櫛比,但當我更新其他行時,它會更新。而且,當我向上滾動表格視圖時,它顯示索引0在所有索引值。這裏是我的代碼
- (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] autorelease];
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:@"Arial" size:16];
[cell addSubview:nameLabel];
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal];
[cell addSubview:deleteButton];
priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:priceMark];
qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:qtyLabel];
}
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"];
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray
objectAtIndex:indexPath.row] objectForKey:@"amount"]];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"];
return cell;
}
哦!我怎麼能做這個愚蠢的錯誤..謝謝! :) – Ketan 2012-02-08 13:26:26
Nikola Kirev,我面臨着同樣的問題,但很少有數據是動態的,必須根據數據添加ImageView。那麼在這種情況下應該怎麼做? – Sandy 2014-03-28 12:26:08