不是一个全职的ios开发人员,需要让这个工作很抱歉,如果看起来很简单。我在自定义UITableViewCell中有以下内容,但它似乎并没有起作用。绝大多数选择器都在Controller中。这可能吗?我在这里做错了什么?
在我的自定义UITableViewCell
中-(void)updateCell:(NSDictionary *)content
{
UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0f, 20.0f)];
mainLabel.text=[content objectForKey:@"name"];
[self.contentView addSubview:mainLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
[self.contentView addSubview:button];
}
-(void)aMethod:(id)sender
{
NSLog(@"here is a method");
}
在我的ViewController中的cellForRowAtIndexPath中调用。
事先提前基本上我想获得类似于此的扩展内容效果:http://jsfiddle.net/bso3sa07/
在iOS方面,我(位于https://github.com/trestles/testview):
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
ViewController.m
#import "ViewController.h"
#import "ContentCell.h"
@interface ViewController (){
NSMutableArray *_data;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource=self;
self.tableView.delegate=self;
NSDictionary *obj1=@{@"name":@"Julie"};
NSDictionary *obj2=@{@"name":@"Melissa"};
_data=[[NSMutableArray alloc] init];
[_data addObject:obj1];
[_data addObject:obj2];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_data count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
ContentCell *cell=(ContentCell *)tmpCell;
return cell.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"ContentCell";
ContentCell *cell = (ContentCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell updateCell:[_data objectAtIndex:indexPath.row]];
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[moreButton addTarget:self
action:@selector(vcSelector)
forControlEvents:UIControlEventTouchUpInside];
[moreButton setTitle:@"vcMore" forState:UIControlStateNormal];
moreButton.frame = CGRectMake(200.0f, 10.0f, 100.0f, 20.0f);
[cell.contentView addSubview:moreButton];
return cell;
}
-(void)vcSelector
{
NSLog(@"hello from vcSelector");
// lets get a reference to that specific cell
// how would I expand that specific cell?
/*
id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
ContentCell *cell=(ContentCell *)tmpCell;
return cell.height;
*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ContentCell.h
#import <UIKit/UIKit.h>
@interface ContentCell : UITableViewCell
@property (nonatomic, assign) CGFloat height;
-(void)updateCell:(NSDictionary *)obj;
@end
ContentCell.m
#import "ContentCell.h"
@implementation ContentCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
-(void)updateCell:(NSDictionary *)content
{
self.height=60.0f;
UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0f, 20.0f)];
mainLabel.text=[content objectForKey:@"name"];
mainLabel.layer.borderColor=[UIColor greenColor].CGColor;
mainLabel.layer.borderWidth=2.0f;
[self.contentView addSubview:mainLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 20.0f, 100.0f, 20.0f);
[self.contentView addSubview:button];
}
-(void)aMethod:(id)sender
{
NSLog(@"here is a method");
UIView *pushView=[[UIView alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 300.0f, 5.0f)];
[pushView setBackgroundColor:[UIColor redColor]];
[self.contentView addSubview:pushView];
self.height=90.0f;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
答案 0 :(得分:0)
尝试这样的事情:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
// Check if button exist already
UIButton *button = (UIButton*)[cell viewWithTag:555];
// if not - create new one
if (!button)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 555;
button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
[cell addSubview:button];
}
// update title and selector if required
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
return cell;
}