我用的是storyboard,在其中一个viewcontroller中拖入tableview,并与对应的类进行链接,但是在运行时,numberOfSectionsInTableView和numberOfRowsInSection均可执行,但cellForRowAtIndexPath就是不执行,界面也不能显示tableview的内容,求解啊!
#pragma address table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger number = 5;
// dropDownOpen = true;
// if (dropDownOpen) {
// number = [addressArray count];
// }
return number;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *DropDownCellIdentifier = @"DropDownCellIdentifier";
switch ([indexPath row]) {
case 0: {
DropDownCell *cell = (DropDownCell*) [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];
if (cell == nil){
NSLog(@"New Cell Made");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DropDownCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[DropDownCell class]])
{
cell = (DropDownCell *)currentObject;
break;
}
}
}
[[cell textLabel] setText:@"Option 1"];
dropDown = @"Option 1";
// Configure the cell.
return cell;
break;
}
default: {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *label = [NSString stringWithFormat:@"Option %i", [indexPath row]];
[[cell textLabel] setText:label];
// Configure the cell.
return cell;
break;
}
}
}
#pragma address table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ([indexPath row]) {
case 0:
{
DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath];
NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]];
NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];
if ([cell isOpen])
{
[cell setClosed];
dropDownOpen = [cell isOpen];
[tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[cell setOpen];
dropDownOpen = [cell isOpen];
[tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
}
break;
}
default:
{
dropDown = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]];
DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path];
[[cell textLabel] setText:dropDown];
// close the dropdown cell
NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]];
NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];
[cell setClosed];
dropDownOpen = [cell isOpen];
[tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
break;
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
用tag来标记这个tableview。在执行的时候在区分。
先给TableView打上tag
tb.tag = 100;
然后在cellForRowAtIndexPath里面
UITableView *a = (UITableView *)tableView;
if(a.tag ==100){
// 执行你的方法
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。