gpt4 book ai didi

ios - 如何从单元格中删除以前的内容

转载 作者:行者123 更新时间:2023-12-01 16:45:10 25 4
gpt4 key购买 nike

我有带有搜索栏的tableview。我正在显示单元格中的数据数量。我正在使用xib创建单元格。我还在单元格中添加了两个按钮。现在,当我在该时间表中的搜索栏中搜索任何内容时,表格将重新加载,并且单元格按钮将再次添加到该位置。
请参阅以下屏幕截图:
1)第一次创建tableview:

2)搜索数据后:

![enter image description here][2]

我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"];
if (cell == nil) {
NSArray *topLevelObject;
topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil];
cell = [topLevelObject objectAtIndex:0];
cell.backgroundColor=RGB(210, 200, 191);
cell.selectedBackgroundView=[self selectedCellView];
}

if(search==FALSE){
NSLog(@"%d",indexPath.row);
cell.clearsContextBeforeDrawing=YES;
//NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row];
NSArray *temp=[tempDataArray objectAtIndex:indexPath.row];
cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"];
cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"];
cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"];
cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"];
cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"];


UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame=CGRectMake(220, 45, 100, 60);
//[doneButton setTitle:@"pick" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
[doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
[doneButton setTag:indexPath.row];
UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
previewButton.frame=CGRectMake(235, 15, 65, 30);
[previewButton setTitle:@"Preview" forState:UIControlStateNormal];
[previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
[previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[previewButton setTag:indexPath.row];
[cell.contentView addSubview:doneButton];
[cell.contentView addSubview:previewButton];
}
else{
// NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row];
// NSLog(@"dic%@",detailList);
//cell.contentView.clearsContextBeforeDrawing=YES;
cell.clearsContextBeforeDrawing=YES;
NSArray *temp=[searchResultArray objectAtIndex:indexPath.row];
cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"];
cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"];
cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"];
cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"];
cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"];
UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame=CGRectMake(220, 45, 100, 60);
//[doneButton setTitle:@"pick" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
[doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
[doneButton setTag:indexPath.row];
UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
previewButton.frame=CGRectMake(235, 15, 65, 30);
[previewButton setTitle:@"Preview" forState:UIControlStateNormal];
[previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
[previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[previewButton setTag:indexPath.row];
[cell.contentView addSubview:doneButton];
[cell.contentView addSubview:previewButton];
}
return cell;

最佳答案

您必须将要摆脱的nil值或将可重复的代码插入可重用性if(这是:if (cell == nil) {
这里的主要规则是创建一个必须将两种对象分开的单元:
-每个单元格唯一的对象(文本)
-重复的对象(主要是样式对象,例如颜色,背景,阴影,按钮和填充物)

第一组应该在外部可重用性“if” (cell == nil)之外。第二个应该在里面。

在您的情况下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"];
if (cell == nil) {
NSArray *topLevelObject;
topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil];
cell = [topLevelObject objectAtIndex:0];
cell.backgroundColor=RGB(210, 200, 191);
cell.selectedBackgroundView=[self selectedCellView];

UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame=CGRectMake(220, 45, 100, 60);
//[doneButton setTitle:@"pick" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
[doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
[doneButton setTag:indexPath.row];
UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
previewButton.frame=CGRectMake(235, 15, 65, 30);
[previewButton setTitle:@"Preview" forState:UIControlStateNormal];
[previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
[previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:doneButton];
[cell.contentView addSubview:previewButton];
}

if(search==FALSE){
NSLog(@"%d",indexPath.row);
cell.clearsContextBeforeDrawing=YES;
//NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row];
NSArray *temp=[tempDataArray objectAtIndex:indexPath.row];
cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"];
cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"];
cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"];
cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"];
cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"];


[previewButton setTag:indexPath.row];

}
else{
// NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row];
// NSLog(@"dic%@",detailList);
//cell.contentView.clearsContextBeforeDrawing=YES;
cell.clearsContextBeforeDrawing=YES;
NSArray *temp=[searchResultArray objectAtIndex:indexPath.row];
cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"];
cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"];
cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"];
cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"];
cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"];

[previewButton setTag:indexPath.row];
}
return cell;

关于ios - 如何从单元格中删除以前的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20608992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com