我使用了一个UITableView,我构建了一个customCell,UITableView连接到NavigationController。
我希望单元格1-3使用navigationBar (例如,单击单元格传递到下一个视图),单元格4-5将保持为可点击的单元格(例如,我单击此单元格和单元格背景更改)。
在storyBoard中,标识符等于Cell。现在,任何行动都会把我转到另一个视野,一个人能帮上忙吗?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}发布于 2014-07-28 13:48:27
在声明cellForRowAtIndexPath时,请确保将自定义单元格添加为:
YourCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果不需要自定义单元格,只需将其保留为UITableViewCell即可。
对操作使用didSelectCellAtIndexPath方法:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *segueIdentifier = [[NSString alloc]init];
switch (indexPath.row) {
case 0:
segueIdentifier = @"segue1";
break;
case 1:
segueIdentifier = @"segue2";
break;
case 2:
segueIdentifier = @"segue3";
break;
if (indexPath.row != 3 && indexPath.row !=4) {
[self performSegueWithIdentifier:segueIdentifier sender:self];
[self dismissViewControllerAnimated:YES completion:nil];
}
}发布于 2014-07-28 13:23:27
为此,您可以使用(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法。
在tableviewcontroller中实现此方法,并在此方法中执行以下操作。
如果您在视图控制器中添加了tableview,那么执行以下操作-
1)在.h文件中添加委托@interface TestTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
2)在.m文件中添加(void)viewDidLoad()方法
self.tableView.delegate=self; self.tableView.dataSource=self;
如果用户单击1-3单元格,调用[self performSegueWithIdentifier:SEGUE_SHOWDETAILS sender:self];in didSelectRowAtIndexPath方法重定向到新页面。
对于4~5个细胞,用didSelectRowAtIndexPath方法改变细胞背景颜色.
https://stackoverflow.com/questions/24996151
复制相似问题