在我的应用程序中,我使用此方法自定义表格视图: -
// 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;
}
我在一行中添加了2个按钮并为每个按钮添加了图像。我有一个按钮,现在我想要的是当我点击按钮时我想要更改按钮中的图像以及表格视图必须改变它在视图中的位置。如何实现这一点。关于按钮点击事件我必须给表视图新框架,并添加新图像到按钮,然后调用表视图的重新加载数据。我试过这个但是什么都没发生。可能是什么问题?请帮忙。非常感谢。
谢谢,
克里斯蒂
答案 0 :(得分:0)
在didselectrowatindexpath中,选择selectedIndex。然后使用reloadData方法重新加载它。在cellforRowatIndexPath中,您必须更改按钮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];
}