我正在尝试在我的应用中创建即将发生的事件表,但是当我选择一行时,indexPathForSelectedRow始终返回0.
NSArray *upcommingTitle;
NSMutableArray *upcommingSubtitle;
NSMutableArray *upcommingTime;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)datesTable numberOfRowsInSection:(NSInteger)section
{
return upcommingTitle.count;
}
-(UITableViewCell *)tableView:(UITableView *)datesTable cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
DatesCustomCell *Cell = [datesTable dequeueReusableCellWithIdentifier:cellIdentifier];
if(!Cell)
{
Cell = [[DatesCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Cell.DatesTitle.text = [upcommingTitle objectAtIndex:indexPath.row];
Cell.DatesSubtitle.text = [upcommingSubtitle objectAtIndex:indexPath.row];
Cell.DatesTime.text = [upcommingTime objectAtIndex:indexPath.row];
return Cell;
}
self.datesTable.dataSource = self;
self.datesTable.delegate = self;
查看已加载
upcommingTitle = [[NSMutableArray alloc] initWithObjects:@"Title1", @"Title2", nil];
upcommingSubtitle = [[NSMutableArray alloc] initWithObjects:@"Sub1", @"Sub2", nil];
upcommingTime = [[NSMutableArray alloc] initWithObjects:@"Date1", @"Date2", nil];
但是以下内容总是返回0,导致“test”标签为“Title1”
查看已显示
_test.text = [upcommingTitle objectAtIndex:self.datesTable.indexPathForSelectedRow.row];
谢谢你的帮助
答案 0 :(得分:1)
尝试在didSelect方法中推送另一个视图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here, indexpath returns the selection of cell's indexpath, so put your code here
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
//then push your view
}
更新:然后使用此代码
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier]isEqualToString:@"pushView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
PushingViewController *pushView = [segue destinationViewController];
pushView._test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
}
答案 1 :(得分:0)
(void)tableView:(UITableView *)datesTable didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_test.text = [upcommingTitle objectAtIndex:indexPath.row];
}
Just put this code in your application.
答案 2 :(得分:0)
在提出定单之前,不要取消选择单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Call This Line
[self performSegueWithIdentifier:@"ShowMobileShopStore" sender:nil];
//Before This
[tableView deselectRowAtIndexPath:indexPath animated:true];
}
答案 3 :(得分:0)
最初的问题是:indexPathForSelectedRow返回0(值不正确),
好吧,这是设计使然,从苹果开发人员的笔记中,该值返回一个IndexPath,该索引路径可以为NULL(这就是您所看到的)。
所以,做正确的事:
1)您需要先测试indexPathForSelectedRow是否不是NULL
2)如果不为NULL,则它将包含(.row),然后将为您提供选定的行索引,该索引从0开始。
3)请注意,如果indexPathForSelectedRow为NULL,indexPathForSelectedRow.row将返回零。
这是我经过测试并可以正常工作的代码:
if (MyTableViewList.indexPathForSelectedRow)
{
long SelectedRow = MyTableViewList.indexPathForSelectedRow.row;
}
我希望这会有所帮助,