在表视图中的单元格上添加标签,并在buttonClicked方法上访问它... :)

时间:2011-10-22 12:25:45

标签: iphone ios

我是一个新的程序员....我想在表视图中的单元格上添加两个标签.....并在buttonClicked方法中访问label.text .....

我是新的程序员。所以我不知道在表格单元格上添加标签....我是对的吗?

提前感谢...为我的代码提供宝贵的时间......:)

 ///insert individual row into the table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

}


cell.selectionStyle = UITableViewCellSelectionStyleNone;//my code.... change the color or selected cell



search_items *pro = [searchCount objectAtIndex:[indexPath row]];



   /// i wants access these two labels on buttonClicked Method...


UILabel *showroomName = [[UILabel alloc] initWithFrame:anySize];

[showroomName setText:pro.s_showroomName];

[cell addSubview:showroomName];

[showroomName release];


UILabel *productName = [[UILabel alloc] initWithFrame:anySize];

[productName setText:pro.s_productName];

[cell addSubview:productName];

[productName release];

   UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

aButton.frame = CGRectMake(150, 15,150,40); 

[aButton setTag:indexPath.row];

    [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:aButton];

UIImage *img = [UIImage imageNamed:@"image.png"];

    [aButton setImage:img forState:UIControlStateNormal];

    [aButton setTitle:@"View Details" forState:UIControlStateNormal];

[aButton addSubview:buttonLabel1];


NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"];

    UIImage *image=[UIImage imageWithContentsOfFile:filepath];



cell.imageView.image=image;



return cell;

}

-(void)buttonClicked:(UIButton*)sender {

    //int tag = sender.tag;


}

1 个答案:

答案 0 :(得分:2)

UILabel *showroomName = [[UILabel alloc] initWithFrame:anySize];

[showroomName setText:pro.s_showroomName];

cell.accessoryView = showroomName;

尝试使用此内存管理:在一次被调用的方法中分配标签(如viewDidLoadviewWillAppearviewDidAppearinit,甚至{{ 1}})并在numberOfSectionInTableView中更改它的文本或任何您想要更改的内容。这样,您就不会出现泄漏,内存管理不良或分配的无用内存。并且,在重新发布时要非常小心。当您确定不需要它们时,应该释放这些对象。释放后,某些对象可能仍然需要它们。