我對iPod的編程還很陌生,所以請耐心等待。我是否需要爲此構建自定義的UITableViewCell?
我試圖在cellView中有按鈕,每個單元格有2個值中的一個,a +和a - 。意思是,按鈕會顯示+,當你點擊它時,它會跳到 - 。
我設法讓按鈕工作並更改底層數據,但我在重繪表時遇到了麻煩。
我添加爲註釋,我需要將該按鈕的文本更改爲源代碼。這可以做到,或者我必須建立一個自定義的uitableviewcell?
Tia,Stefano。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
//some code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SectionsTableIdentifier] autorelease];
UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height);
[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
if (some condition) [button setTitle:@"+" forState:UIControlStateNormal];
else [button setTitle:@"-" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
}
if (someCondition) {
cell.textLabel.text = @"text1";
}else {
cell.textLabel.text = @"text2";
}
//I NEED TO CHANGE THE TEXT FOR THE BUTTON HERE.
return cell;
}
哦,我錯過了這個答案,或者我wouldn't've貼我的。 Upticked。 – 2010-11-16 23:22:56
接受Steven's的回答只是因爲它早些發佈。 完美的作品,非常感謝。學習Objective-C和Iphone API同時有時候是一種壓倒性的。 – blindstuff 2010-11-17 14:58:30