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

我是一个新的程序员….我想添加两个标签的单元格在表视图…..和访问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; } 

 UILabel *showroomName = [[UILabel alloc] initWithFrame:anySize]; [showroomName setText:pro.s_showroomName]; cell.accessoryView = showroomName; 

试试这个内存pipe理:在一个被称为一次的方法(如viewDidLoadviewWillAppearviewDidAppearinit ,甚至numberOfSectionInTableView )中分配标签,只是改变它的文本或任何你想改变cellForRowAtIndexPath 。 这样,你不会有泄漏,不良的内存pipe理或分配无用的内存。 而且,还要谨慎的做法。 当你确定你不需要这些东西时,你应该释放这些东西。 释放它们之后,某些对象可能仍然需要它们。