在桌面视图页脚上,我有一个按钮。单击页脚按钮将重新加载表数据。但是当重新加载数据时,表格的数据再次显示在其中cameraBtn
从UIImagePicker
视图控制器中选择图像并将其显示在图像视图上。但我想重新加载表时无法显示表的图像。任何人都可以帮助我。我使用以下代码。
.h文件
{
BOOL isBtnClicked; // in .h file
}
.m文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *subviewArray = cell.contentView.subviews;
for (UIView *view in subviewArray)
{
[view removeFromSuperview];
}
UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
picLabels.text = listItems[indexPath.row];
picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
picLabels.font =[UIFont systemFontOfSize:13];
[cell.contentView addSubview:picLabels];
if (isBtnClicked )
{
imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
}
else
{
imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.image = [imageArray objectAtIndex:indexPath.row];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
}
parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
parcelButton.frame = CGRectMake(180, 0, 50, 50);
[parcelButton setBackgroundImage:[UIImage imageNamed:@"Camera.png"]forState:UIControlStateNormal];
parcelButton.tag = indexPath.row;
[parcelButton addTarget:self action:@selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = parcelButton;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
mainview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[mainview setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
createDelvTableView.tableFooterView = mainview;
UIButton *addSection = [[UIButton alloc]initWithFrame:CGRectMake(270, 0, 40, 40)];
isBtnClicked = YES;
[addSection setBackgroundImage:[UIImage imageNamed:@"addSection.png"] forState:UIControlStateNormal];
[addSection addTarget:self action:@selector(addSectionAction) forControlEvents:UIControlEventTouchUpInside];
[mainview addSubview:addSection];
return mainview;
}
- (void)addPictureAction:(UIButton*)image
{
NSLog(@"Add Pictures");
isBtnClicked = NO;
index=image.tag;
// if ([UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.allowsEditing = YES;
photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:photoPicker animated:YES completion:NULL];
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
- (void)addSectionAction
{
NSLog(@"ADD section");
indexes++;
isBtnClicked = YES;
[createDelvTableView reloadData];
}
提前致谢。
答案 0 :(得分:2)
我无法理解为什么一次又一次地在单元格上添加对象..然后将其删除..修改 cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
picLabels.font =[UIFont systemFontOfSize:13];
picLabels.tag = 101;
[cell.contentView addSubview:picLabels];
UIImageView *imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.tag = 102;
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
UIButton *parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
parcelButton.frame = CGRectMake(180, 0, 50, 50);
[parcelButton setBackgroundImage:[UIImage imageNamed:@"Camera.png"]forState:UIControlStateNormal];
parcelButton.tag = indexPath.row;
[parcelButton addTarget:self action:@selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
}
UILabel *lbl = (UILabel*)[cell.contentView viewWithTag:101];
picLabels.text = listItems[indexPath.row];
UIImageView *img = (UIImageView*)[cell.contentView viewWithTag:102];
if (!isBtnClicked )
{
imageParcel.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
}
else
{
imageParcel.image = [UIImage new];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = parcelButton;
return cell;
}
从 viewForFooterInSection
中删除 isBtnClicked = YES;页脚按钮操作:
isBtnClicked = YES;
[yourTblView reloadData];
在ImagePicker按钮操作: -
isBtnClicked = NO;