【问题标题】:Button add as sub view in table view showing in other cells also按钮添加为表格视图中的子视图,也显示在其他单元格中
【发布时间】:2013-02-22 07:03:21
【问题描述】:

我有表格视图,在第二部分的行中我放置了一个按钮,当我滚动表格视图时,此按钮也会出现在其他行中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];/////self...ramesh
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID];
    }

    if (indexPath.row==1)
    {
        cell.accessoryView=[[UIView alloc]initWithFrame:self.warmUpSwitch.frame];
        cell.textLabel.text=@"Warm Up(5 min.)";
        [cell.accessoryView addSubview:self.warmUpSwitch];
    }
    if (indexPath.row==2)
    {
        cell.accessoryView=[[UIView alloc]initWithFrame:self.coolDownSwitch.frame];

        cell.textLabel.text=@"Cool Down(5 min.)";
        [cell.accessoryView addSubview:self.coolDownSwitch];
    }
}

【问题讨论】:

  • 你能展示你的代码吗,我怀疑你对所有单元格都使用了可重用性,并为不需要的单元格获取按钮
  • 虽然这不是一个好方法,但将可重用标识符设置为 nil 会对您有所帮助。
  • 请发布您的代码如何添加按钮
  • 我已经添加了代码,请检查一下。
  • 你能发布整个方法如何初始化单元格吗?

标签: iphone object user-interface location


【解决方案1】:

试试这个,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


}
  if (indexPath.row==1)
    {

     UIButton *btnwarmUpSwitch = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
     btnwarmUpSwitch.frame = CGRectMake(250,20,100,50);  //set frame where you want to set
    [btnwarmUpSwitch setTitle:@"Warm Up(5 min.)" forState:UIControlStateNormal];
     btnwarmUpSwitch.tag=indexPath.row;
    [btnwarmUpSwitch addTarget:self action:@selector(selectwarmUpSwitch:) forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:btnwarmUpSwitch];


    }
    if (indexPath.row==2)
    {
        UIButton *btncoolDownSwitch = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
     btncoolDownSwitch.frame = CGRectMake(250,20,100,50);  //set frame where you want to set
    [btncoolDownSwitch setTitle:@"Cool Down(5 min.)" forState:UIControlStateNormal];
     btncoolDownSwitch.tag=indexPath.row;
    [btncoolDownSwitch addTarget:self action:@selector(selectcoolDownSwitch:) forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:btncoolDownSwitch];
    }
   return cell;
 }
 - (void) selectwarmUpSwitch:(id)sender
 {

 }
 - (void) selectcoolDownSwitch:(id)sender
 {

 }

【讨论】:

  • 我做了一些改变,但是这个过度显示文本字段并在文本字段中切换的问题仍然存在。请告诉我使用标签的好处,意思是 textfield.tag。
  • 还有一个条件可能会有所帮助,我已将表格视图添加为子视图,并在其上添加了按钮和文本字段,可能是这件事没有清除单元格的可重用性跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多