目标c-通过单击xcode中的按钮来更改tabel视图的内容和位置



在我的应用程序中,我使用以下方法自定义我的表视图:-

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";      
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // if (cell == nil) {
    // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    // }
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if(indexPath.row == 0)
    {                       
        button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
        [button1 setImage:[UIImage imageNamed:@"BUTTON.png"] forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:self.button1];
        button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
        [button2 setImage:[UIImage imageNamed:@"TURN OFF.png"] forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button2];
    }
    else if(indexPath.row == 1)
    {
        button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
        [button3 setImage:[UIImage imageNamed:@"JUMP.png"] forState:UIControlStateNormal];
        [button3 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button3];
        button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
        [button4 setImage:[UIImage imageNamed:@"CLOSE.png"] forState:UIControlStateNormal];
        [button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button4];
    }
    else
    {
        // Do something here
    }
    return cell;
}

我在一行中添加了两个按钮,并为每个按钮添加了图像。我有一个按钮,现在我想要的是,当我点击按钮时,我想改变按钮中的图像,而且表格视图必须改变它在视图中的位置。如何实现这一点。在按钮点击事件上,我必须为表视图提供新的框架,并向按钮添加新的图像,然后为表视图调用重载数据。我尝试过,但没有发生任何事情。可能是什么问题?请帮忙。任何帮助都将不胜感激。

谢谢,

Christy

在didselecrowatindexpath中获取selectedIndex。然后使用reloadData方法可以重新加载它。在RowatIndexPath的单元格中,您必须更改按钮Image。

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    //change background image here
    [button setBackgroundImage:[UIImage imageNamed:@"ima.jpg"]];
    return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selected=indexPath.row;//selected is an integer
        [tableview reloadData];
}

最新更新