【发布时间】:2013-05-01 01:50:30
【问题描述】:
试图确定为什么我在didSelectRowAtIndexPath. 中被选中后无法显示我的新“艺术”菜单我意识到它没有被正确使用......还有,有什么方法可以让它在之后显示轻按两次,还是 didSelectRowAtIndexPath 默认只按一次?
-(void)viewDidAppear:(BOOL)animated
{
NSString *arts = @"Arts and Museums";
[arrayNo addObject:arts];
[[self myTableView] reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[[self myTableView] setDelegate:self];
[[self myTableView] setDataSource:self];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayNo count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
NSLog(@"CREATING NEW CELL");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0];
}
cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
{
NSString *galleries = @"Art Galleries";
NSString *dramatic = @"Dramatic Arts";
NSString *museums = @"Museums";
[arrayNo addObject:galleries];
[arrayNo addObject:dramatic];
[arrayNo addObject:museums];
[self.myTableView reloadData];
}
}
************ 更新*************
用下面的代码更新我的didSelectRowAtIndexPath 后,我看到它正在引用它,但是我的新菜单项没有弹出来替换旧数组。我尝试使用self.myTableView = nil 并重新加载数据,但它似乎没有效果?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.textLabel.text isEqualToString: @"Arts and Museums"])
{
NSString *galleries = @"Art Galleries";
NSString *dramatic = @"Dramatic Arts";
NSString *museums = @"Museums";
[arrayNo addObject:galleries];
[arrayNo addObject:dramatic];
[arrayNo addObject:museums];
NSLog(@"LOADED");
}
}
【问题讨论】:
-
您是否尝试过调试?是否进入
didSelectRowAtIndexPath:方法? -
如果 didSelectRowAtIndexPath 没有触发,很可能是您没有正确设置 tableview 的委托。至于双击,您可能需要使用 uigesturerecognizer 来实现自己的自定义操作方法来检测点击。
-
你还有没有[self.myTableView reloadData];在更新的 didSelectRowAtIndexPath 方法结束时?
-
@rdelmar 我做到了。两种方式都试过了,但没有任何效果。当我按下单元格时,水龙头显然在工作,因为我可以通过 nslog 看到,但无论出于何种原因,初始数组都不会清除
-
代替 NSLog(@"LOADED");,记录 NSLog(@"%@", ArrayNo) 看看是否显示所有对象。
标签: ios objective-c uitableview