我使用了UITableView
,构建了customCell
,UITableView
连接到NavigationController
。
我希望单元格1-3使用navigationBar
(例如,单击单元格进入下一个视图(而单元格4-5只是保持为可点击的单元格(例如,我点击这个单元格,单元格背景发生变化(。
在故事板中,标识符等于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;
}
在声明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];
}
}
您可以使用(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
方法。
在您的tableviewcontroller中实现此方法,并在此方法中执行以下操作。
如果您已经在viewcontroller中添加了tableview,那么请执行以下操作-
1( 在.h文件中添加委托@interface TestTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
2( 在(void)viewDidLoad()
方法中的.m文件中添加self.tableView.delegate=self;
self.tableView.dataSource=self;
如果用户点击1-3个单元格,则在didSelectRowAtIndexPath方法中调用[self performSegueWithIdentifier:SEGUE_SHOWDETAILS sender:self];
重定向到新页面
并且对于4-5个细胞,在didSelectRowAtIndexPath方法中改变细胞背景颜色。