gpt4 book ai didi

iOS:在 UITableview 中以编程方式创建的按钮重复两次?

转载 作者:行者123 更新时间:2023-12-01 18:15:42 25 4
gpt4 key购买 nike

我的项目基于解析 XML 数据和显示屏幕,在我的项目中,我需要在表格 View 单元格内编程设置 UIButton。我做到了,但是当我运行它时,按钮会重复两次。像这样
enter image description here

我不知道要解决这个问题这是我尝试过的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return [eventarray count];
if (section == 1)
return 1;
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"eventCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(indexPath.section == 0)
{
Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
cell.textLabel.text = msglist.invitationdet;
NSLog(@"Array %@",[SingleTonClass sinlgeTon].colorArray);
NSInteger stat=msglist.readflag;
if([[SingleTonClass sinlgeTon].colorArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat == 1 ) {
NSInteger stat1 = msglist.responseflag;
if(stat1 == 1){
cell.textLabel.textColor = [UIColor yellowColor];
}
else {
cell.textLabel.textColor = [UIColor redColor];
}
}
else{
cell.textLabel.textColor = [UIColor greenColor];
}
cell.backgroundColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}
if(indexPath.section == 1)
{
UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
[viewmoreButton setTitle:@"View More" forState:UIControlStateNormal];
[cell addSubview:viewmoreButton];
[viewmoreButton addTarget:self
action:@selector(viewMore:)
forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [ UIColor blackColor];
}
return cell;
}

该按钮必须仅显示在第二个单元格上,请帮助我如何解决此问题 提前致谢

最佳答案

我认为问题在于您将按钮添加到 UITableViewCell ,然后当 reloadData在 table 上调用时,该单元格将排队等待重用,而该按钮仍附加在该按钮上。

创建您自己的UITableViewCell子类并将其用于需要自定义按钮的表格部分。

关于iOS:在 UITableview 中以编程方式创建的按钮重复两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com