0
我有一個表單調用單獨的UITableViewCell
。我有代碼來佈局UITableViewCell
中的單元格,並在我的tableview中正確顯示。不過,由於某些原因,如果我選擇單元格,單元格中的內容將重繪在單元格的頂部。TableViewCell內容在選中單元格時重複
有誰知道這是爲什麼?我附上了代碼來繪製下面的單元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == productInformationTable){
if (indexPath.row == 0) {
static NSString *CellIdentifier = @"Cell";
ProductImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else{
cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
else if (indexPath.row == 1) {
static NSString *CellIdentifier = @"Cell";
ProductQuantityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil) {
cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else{
cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
else if (indexPath.row == 2) {
static NSString *CellIdentifier = @"Cell";
ProductButtonsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else{
cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
else {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
}
else {
return nil;
}
}
我的細胞的代碼如下 -
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.contentView.backgroundColor = [UIColor redColor];
customButton = [[SAExpandableButton alloc]initWithFrame:CGRectMake(self.frame.size.width - 50, self.frame.size.height/2 - 15, 30, 30)];
customButton.layer.borderColor = [Styles priceRed].CGColor;
customButton.layer.borderWidth = 1.5;
customButton.layer.cornerRadius = 15;
customButton.expandDirection = SAExpandDirectionLeft;
customButton.numberOfButtons = 8;
customButton.selectedIndex = 0;
quantityLabel = [UILabel new];
quantityLabel.frame = CGRectMake(10, self.frame.size.height/2 - 10, 100, 20);
quantityLabel.text = @"Quantity";
[self addSubview:quantityLabel];
customButton.buttonTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];
[self addSubview:customButton];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
return nil;
}
return indexPath;
NSLog(@"willSelectRow called");
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Table Pressed");
}
嗯...不是沒有看到您的代碼。 – rdelmar 2014-10-01 05:14:05
請提供您的相關代碼部分,以便我們可以調查問題。 – fluidsonic 2014-10-01 05:14:13
對不起,我現在就加入它。我知道它必須是因爲layoutSubviews每次都被調用,但我不明白爲什麼。 – crashlog 2014-10-01 05:22:45