gpt4 book ai didi

iphone - 使用默认方法删除 uitableview 行时遇到问题

转载 作者:行者123 更新时间:2023-11-28 20:30:30 25 4
gpt4 key购买 nike

这个 UITableViews 会让我发疯!

我有 UITableViewCell 创建者

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
[cell addSubview:doneButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
[doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
postedTime.backgroundColor = [UIColor clearColor];
[cell addSubview:postedTime];
cell.textLabel.textColor = [UIColor blackColor];

UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 190, 30)];
[cell addSubview:post];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
[cell addSubview:title];

mission *m = (mission *)[missionsArray objectAtIndex:indexPath.row];
title.text = m.title;
post.text = m.post;
postedTime.text = m.posted;

.h文件:

IBOutlet UITableView *table;
NSMutableArray *missionsArray;

我正在尝试通过方法删除行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[missionsArray removeObjectAtIndex:indexPath.row];
[table reloadData];
}

当我单击“删除”按钮时,EXC_BAD_ACCESS。我试过调用 [missionsArray removeAllObject]; ——同样的错误。然后我尝试了 NSLog(@"%u", indexPath.row); -- 它打印正确的行(当我选择第一行时,它返回 0 等等)。我也试过这个

if (editingStyle == UITableViewCellEditingStyleDelete) {
[missionsArray removeObjectAtIndex:indexPath.row];
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[table reloadData];
}

而且我也没有什么好结果。也许细胞高度会影响它......请帮助。我不知道该做什么,去哪里搜索。


问题解决了。当我试图释放我的任务对象时,问题出在逻辑错误上。

最佳答案

#import "ViewController.h"


@implementation ViewController

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
missionsArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C", nil];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1 {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return [missionsArray count];
}


- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 44;


}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}

UIButton * doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
[cell addSubview:doneButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
[doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
postedTime.backgroundColor = [UIColor clearColor];
[cell addSubview:postedTime];
cell.textLabel.textColor = [UIColor blackColor];

UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 20, 190, 30)];

post.text=[missionsArray objectAtIndex:indexPath.row];
[cell addSubview:post];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
[cell addSubview:title];

return cell;

}

enter code here

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[missionsArray removeObjectAtIndex:indexPath.row];
[table reloadData];
}
}




@end

关于iphone - 使用默认方法删除 uitableview 行时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12316104/

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