2013-01-11 118 views
0

我在tableview中使用8個單元格。每個單元都有一組組件,如UIButton,UILabel和UISlider。當我更新第六個單元格時,它反映到第一個單元格。同樣,如果我更新第7個單元格,它會反映到第2個單元格。UITableviewcell內容更新反映到其他單元格

刪除單元代碼:

{ 
[buttonarray removeObjectAtIndex:delpath.row]; 
viewcount--; 

ListCell1 *cell=(ListCell1 *)[table cellForRowAtIndexPath:delpath]; 

cell.slider.value=0; 
[email protected]"1"; 
[email protected]"1"; 
cell.n=1; 
[cell.button1 setTitle:@"240" forState:UIControlStateNormal]; 


for (int m=0; m<7; m++) 
{ 
    [[cell.myarray objectAtIndex:m]setImage:[UIImage imageNamed:@"Num2.png"] forState:UIControlStateNormal]; 
    UIButton *but=[cell.myarray objectAtIndex:m]; 
    but.tag=0; 
} 

[self->table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:delpath, nil]  withRowAnimation:UITableViewRowAnimationFade]; 
[table reloadData]; 
delpath=NULL; 
} 

按下按鈕時

-(void)buttonpressed:(id)sender 
{ 
    viewcount++; 
table.delegate=self; 
table.dataSource=self; 
//UIView *middleview=[[UIView alloc]initWithFrame:CGRectMake(100,200,300,200)]; 
[buttonarray addObject:sender]; 
[table reloadData]; 
} 

代碼實現代碼如下

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return viewcount; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *[email protected]"mycell"; 

ListCell1 *cell = (ListCell1 *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) 
{ 
cell = [[ListCell1 alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]; 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
UIButton *b=[buttonarray objectAtIndex:indexPath.row]; 
cell.imageView.frame=CGRectMake(0, 0, 0, 0); 
b.imageView.frame=CGRectMake(0, 0, 30, 30); 
cell.imageView.image=b.imageView.image; 
return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
ListCell1 *cell1 = (ListCell1 *)[tableView cellForRowAtIndexPath:indexPath]; 
[cell1 addSubview:delbutton]; 
delpath=indexPath; 
return; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 100.0; 
} 
+2

***與Xcode無關***。 – 2013-01-11 12:33:45

+0

haha​​a ... sory frndz我不明白你的代碼:(所以請..輸出更清晰的代碼:)謝謝:) – iPatel

+0

你可以給我一個示例代碼,以添加在單元格中的comp和如何刪除該單元格沒有任何改變值 – Krishnan8190

回答

2

此問題是因爲UIKit的再利用細胞一個UITableView。因此,當您將UI組件添加到該單元格時,相同的元素將在不同的行上重新使用。

簡而言之,每次更新單元時都需要清除/重新構建UI。

+0

我在這裏遇到了同樣的問題。那麼我怎樣才能以編程方式解決它 – gamal

相關問題