我写了一个 UITableView(我为演示硬编码了一些东西),有 20 行。我将删除按钮的文本更改为“删除”,默认为“删除”。我创建了两个按钮“顶部”和“底部”来将表格视图滚动到顶部和底部。当我滑动第 1 行时,会出现“删除”按钮。之后,我触摸底部按钮将表格视图滚动到底部。然后我触摸顶部按钮再次滚动到顶部。现在,我的删除按钮的文本更改为“删除”(默认)。你能帮我解决这个错误吗?
#import "ViewController.h"
#import <CoreData/CoreData.h>
@interface ViewController (){
UITableView* uitableView;
UIButton* goToTopButton;
UIButton* goToBottomButton;
}
- (void) goToTopButtonTouched;
- (void) goToBottomButtonTouched;
@end
@implementation ViewController
- (void)loadView{
[super loadView];
self.view.backgroundColor = [UIColor lightGrayColor];
uitableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
uitableView.delegate = self;
uitableView.dataSource = self;
[self.view addSubview:uitableView];
goToTopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
goToTopButton.frame = CGRectMake(0, 0, 80, 30);
[goToTopButton setTitle:@"Top" forState:UIControlStateNormal];
[goToTopButton addTarget:self action:@selector(goToTopButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goToTopButton];
goToBottomButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
goToBottomButton.frame = CGRectMake(100, 0, 80, 30);
[goToBottomButton setTitle:@"Bottom" forState:UIControlStateNormal];
[goToBottomButton addTarget:self action:@selector(goToBottomButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goToBottomButton];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSLog(@"MEMORY WARING");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* cellReuseId = @"cellReuse";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellReuseId];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseId];
}
[cell.textLabel setText:[NSString stringWithFormat:@"%d", indexPath.row]];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"Remove";
}
- (void)goToTopButtonTouched{
[uitableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
- (void)goToBottomButtonTouched{
[uitableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:19 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
@end